-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_view.py
1521 lines (1297 loc) · 55.3 KB
/
img_view.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
# encoding: utf-8
# https://blog.csdn.net/weixin_44821251/article/details/106290132#comments_18071440
'''
@author: LCS
@file: img_view.py
@time: 2020/5/22 12:12
'''
# important data structures
# rh: loc to routeTile[] at this loc
# th: loc to th_Node[]: ty, id, nets, width
# blk_id_list: id to tileNode(old)
# tr_map: tile_id to routeTile[]
# sg: sg_id to routeTile[]
# net_to_seg: net_id to sg_id
import grid
from PySide2.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox
from PySide2.QtUiTools import QUiLoader
from PySide2.QtGui import QPainter, QBrush, QColor
from PySide2.QtCore import Qt, QRect
from PySide2 import QtWidgets
from PySide2 import QtCore
from PySide2 import QtGui
from pnr_graph import (
RoutingResultGraph,
construct_graph,
TileType,
RouteType,
TileNode,
RouteNode,
)
import pythunder
import pycyclone
import tile_hist
from graph_pro import Design
from pnr_graph import TileType
import time
import string
import os
GLOBAL_TILE_WIDTH = 200
GLOBAL_TILE_MARGIN = 40 #each side is 40 pixs
GLOBAL_TILE_WIDTH_INNER = GLOBAL_TILE_WIDTH - 2 * GLOBAL_TILE_MARGIN
GLOBAL_OFFSET_X = 20 #outer margin
GLOBAL_OFFSET_Y = 20
GLOBAL_NUM_TRACK = 5
GLOBAL_ARROW_DISTANCE = GLOBAL_TILE_WIDTH_INNER // (GLOBAL_NUM_TRACK * 2 + 1)
class PICK_TILE:
def __init__(self, loc, is_tile, is_working_tile):
self.loc = loc
self.is_tile = is_tile
self.is_working_tile = is_working_tile
self.is_valid_target = is_tile and (not is_working_tile)
class potential_sb:
def __init__(self, x, y, io, side, track, width, net):
self.x = x
self.y = y
self.io = io
self.side = side
self.track = track
self.width = width
self.net = net
class INFO_WIN(QWidget):
def __init__(self, label):
super().__init__()
self.label = label
def display_text(self, text):
self.label.setText(text)
return
class IMG_WIN(QWidget):
def __init__(self, qw, graphicsView):
super().__init__()
self.graphicsView=graphicsView
self.p = qw
self.graphicsView.setStyleSheet("padding: 0px; border: 0px;") # 内边距和边界去除
self.scene = QtWidgets.QGraphicsScene(self)
self.graphicsView.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) # 改变对齐方式
self.graphicsView.setSceneRect(0, 0, self.graphicsView.viewport().width(),
self.graphicsView.height()) # 设置图形场景大小和图形视图大小一致
self.graphicsView.setScene(self.scene)
self.scene.mousePressEvent = self.scene_MousePressEvent # 接管图形场景的鼠标事件
# self.scene.mouseReleaseEvent = self.scene_mouseReleaseEvent
self.scene.mouseMoveEvent = self.scene_mouseMoveEvent
self.scene.wheelEvent = self.scene_wheelEvent
self.ratio = 1 # 缩放初始比例
self.zoom_step = 0.1 # 缩放步长
self.zoom_max = 3 # 缩放最大值
self.zoom_min = 0.3 # 缩放最小值
self.pixmapItem=None
# self.tile_x = 5
# self.tile_y = 4
# self.img_draw = grid.IMG_DRAW((self.tile_x, self.tile_y))
self.started = False
self.empty_tile = PICK_TILE((-1, -1), False, False)
self.selected_tile = self.empty_tile
self.target_tile = self.empty_tile
######################## IMG CREATION #################
# refresh for the update on the display
def refresh_img(self):
if not self.started:
self.display_message("WARN:","please load the files before start")
return
img = self.img_draw.rt_Qt()
self.org = img
if self.pixmapItem != None:
originX = self.pixmapItem.x()
originY = self.pixmapItem.y()
else:
originX, originY = 0, 0 # 坐标基点
self.scene.clear()
self.pixmap = QtGui.QPixmap.fromImage(img)
self.pixmapItem = self.scene.addPixmap(self.pixmap)
self.pixmapItem.setScale(self.ratio) # 缩放
self.pixmapItem.setPos(originX, originY)
def load_img(self, x, y, count):
self.started = True
self.tile_x = x
self.tile_y = y
self.count = count
self.img_draw = grid.IMG_DRAW((self.tile_x, self.tile_y))
self.img_draw.set_count(self.count)
self.reload_img()
# reload for updates in the design file
def reload_img(self):
if not self.started:
self.display_message("WARN:","please load the files before start")
return
self.img_draw.set_tile_hist(self.p.design.th)
self.img_draw.load_background()
self.img_draw.draw_used_tiles()
# self.img_draw.draw_used_routes(self.p.design.result_graph, self.p.design_width)
self.img_draw.draw_used_routes_n(self.p.design.sg, self.p.design_width, self.p.design.result_graph)
self.img_draw.label_used_tiles()
if self.p.edit_mode and not self.p.change_placement:
self.img_draw.show_pot_sb(self.p.pot_sbs)
img = self.img_draw.rt_Qt()
self.org = img
if self.pixmapItem != None:
originX = self.pixmapItem.x()
originY = self.pixmapItem.y()
else:
originX, originY = 0, 0 # 坐标基点
self.scene.clear()
self.pixmap = QtGui.QPixmap.fromImage(img)
self.pixmapItem = self.scene.addPixmap(self.pixmap)
self.pixmapItem.setScale(self.ratio) # 缩放
self.pixmapItem.setPos(originX, originY)
######################## INTERFACE #################
def display_message(self, title, mesg):
self.p.display_message(title, mesg)
return
def display_text(self, mesg):
st = ""
if self.p.edit_mode:
st += "Edit:\n"
if self.p.change_placement:
st += "P\n"
else:
st += "R\n"
else:
st += "View:\n"
st += "Current Width: " + str(self.p.design_width) + "\n"
st += "========\n"
st += mesg
self.p.display_text(st)
return
def get_tile_info(self, p_tile):
s = "\n"
if p_tile.is_tile:
s = "tile: " + str(p_tile.loc[0]) + " " + str(p_tile.loc[1]) + "\n"
if p_tile.is_working_tile:
info = self.p.design.th[p_tile.loc]
for i in info:
tile_type = i.ty
tile_id = i.id
tile_seg = i.width
s += tile_type.name + ": " + tile_id + "\n" + "width " + str(tile_seg) + ": "
for k in i.nets:
s += str(k) + " "
s += "\n --------\n"
s += "SB ports:\n"
if p_tile.loc in self.p.design.rh:
print(len(self.p.design.rh[p_tile.loc]))
for port in self.p.design.rh[p_tile.loc]:
if port.bit_width == self.p.design_width:
s += port.net_id + " side " + str(port.side) + " track " + str(port.track)
if port.io:
s += " out\n"
else:
s+= " in\n"
return s
def show_selected_tile(self):
s = self.get_tile_info(self.selected_tile)
if self.p.edit_mode and self.p.change_placement:
s += "SET TO:\n"
s += self.get_tile_info(self.target_tile)
self.display_text(s)
def get_tile(self, pos):
tile = (-1, -1)
w = self.pixmap.size().width() * (self.ratio)
h = self.pixmap.size().height() * (self.ratio)
x1 = self.pixmapItem.pos().x() # 图元左位置
x2 = self.pixmapItem.pos().x() + w # 图元右位置
y1 = self.pixmapItem.pos().y() # 图元上位置
y2 = self.pixmapItem.pos().y() + h # 图元下位置
XT = self.tile_x *GLOBAL_TILE_WIDTH
YT = self.tile_y *GLOBAL_TILE_WIDTH
x = pos.x()
y = pos.y()
if x < x1 or x > x2 or y < y1 or y > y2: #outside
return tile
#pos in image space
dx = (x - x1) / self.ratio
dy = (y - y1) / self.ratio
dx -= GLOBAL_OFFSET_X
dy -= GLOBAL_OFFSET_Y
if dx < 0 or dy < 0 or dx > XT or dy > YT:
return tile
else:
px = dx // GLOBAL_TILE_WIDTH
py = dy // GLOBAL_TILE_WIDTH
ddx = dx - px * GLOBAL_TILE_WIDTH
ddy = dy - py * GLOBAL_TILE_WIDTH
out_x = ddx < GLOBAL_TILE_MARGIN or ddx > GLOBAL_TILE_MARGIN + GLOBAL_TILE_WIDTH_INNER
out_y = ddy < GLOBAL_TILE_MARGIN or ddy > GLOBAL_TILE_MARGIN + GLOBAL_TILE_WIDTH_INNER
if out_x or out_y:
return tile
else:
return (dx // GLOBAL_TILE_WIDTH, dy // GLOBAL_TILE_WIDTH)
def get_sb_box_cent(self, sb):
side = sb.side
io = sb.io
tile_x = sb.x
tile_y = sb.y
track_id = sb.track
pw = GLOBAL_TILE_MARGIN * 0.5
if side == 3:
if io == 0:
dir = "DOWN"
x = (
GLOBAL_OFFSET_X
+ GLOBAL_TILE_MARGIN
+ tile_x * GLOBAL_TILE_WIDTH
+ (track_id + 1) * GLOBAL_ARROW_DISTANCE
)
y = GLOBAL_OFFSET_Y + tile_y * GLOBAL_TILE_WIDTH
elif io == 1:
dir = "UP"
x = (
GLOBAL_OFFSET_X
+ GLOBAL_TILE_MARGIN
+ tile_x * GLOBAL_TILE_WIDTH
+ (track_id + 1 + GLOBAL_NUM_TRACK) * GLOBAL_ARROW_DISTANCE
)
y = GLOBAL_OFFSET_Y + GLOBAL_TILE_MARGIN + tile_y * GLOBAL_TILE_WIDTH
elif side == 0:
if io == 0:
dir = "LEFT"
x = GLOBAL_OFFSET_X + tile_x * GLOBAL_TILE_WIDTH + GLOBAL_TILE_WIDTH
y = (
GLOBAL_OFFSET_Y
+ GLOBAL_TILE_MARGIN
+ tile_y * GLOBAL_TILE_WIDTH
+ (track_id + 1) * GLOBAL_ARROW_DISTANCE
)
elif io == 1:
dir = "RIGHT"
x = (
GLOBAL_OFFSET_X
+ tile_x * GLOBAL_TILE_WIDTH
+ GLOBAL_TILE_WIDTH
- GLOBAL_TILE_MARGIN
)
y = (
GLOBAL_OFFSET_Y
+ GLOBAL_TILE_MARGIN
+ tile_y * GLOBAL_TILE_WIDTH
+ (track_id + 1 + GLOBAL_NUM_TRACK) * GLOBAL_ARROW_DISTANCE
)
elif side == 1:
if io == 0:
dir = "UP"
x = (
GLOBAL_OFFSET_X
+ GLOBAL_TILE_MARGIN
+ tile_x * GLOBAL_TILE_WIDTH
+ (track_id + 1 + GLOBAL_NUM_TRACK) * GLOBAL_ARROW_DISTANCE
)
y = GLOBAL_OFFSET_Y + tile_y * GLOBAL_TILE_WIDTH + GLOBAL_TILE_WIDTH
elif io == 1:
dir = "DOWN"
x = (
GLOBAL_OFFSET_X
+ GLOBAL_TILE_MARGIN
+ tile_x * GLOBAL_TILE_WIDTH
+ (track_id + 1) * GLOBAL_ARROW_DISTANCE
)
y = (
GLOBAL_OFFSET_Y
+ tile_y * GLOBAL_TILE_WIDTH
+ GLOBAL_TILE_WIDTH
- GLOBAL_TILE_MARGIN
)
elif side == 2:
if io == 0:
dir = "RIGHT"
x = GLOBAL_OFFSET_X + tile_x * GLOBAL_TILE_WIDTH
y = (
GLOBAL_OFFSET_Y
+ GLOBAL_TILE_MARGIN
+ tile_y * GLOBAL_TILE_WIDTH
+ (track_id + 1 + GLOBAL_NUM_TRACK) * GLOBAL_ARROW_DISTANCE
)
elif io == 1:
dir = "LEFT"
x = GLOBAL_OFFSET_X + tile_x * GLOBAL_TILE_WIDTH + GLOBAL_TILE_MARGIN
y = (
GLOBAL_OFFSET_Y
+ GLOBAL_TILE_MARGIN
+ tile_y * GLOBAL_TILE_WIDTH
+ (track_id + 1) * GLOBAL_ARROW_DISTANCE
)
if dir == "LEFT":
x -= pw
elif dir == "RIGHT":
x += pw
elif dir == "UP":
y -= pw
elif dir == "DOWN":
y += pw
return (x, y)
def check_sb(self, pos):
w = self.pixmap.size().width() * (self.ratio)
h = self.pixmap.size().height() * (self.ratio)
x1 = self.pixmapItem.pos().x() # 图元左位置
x2 = self.pixmapItem.pos().x() + w # 图元右位置
y1 = self.pixmapItem.pos().y() # 图元上位置
y2 = self.pixmapItem.pos().y() + h # 图元下位置
XT = self.tile_x *GLOBAL_TILE_WIDTH
YT = self.tile_y *GLOBAL_TILE_WIDTH
x = pos.x()
y = pos.y()
if x < x1 or x > x2 or y < y1 or y > y2: #outside
return
#pos in image space
dx = (x - x1) / self.ratio
dy = (y - y1) / self.ratio
pw = (GLOBAL_TILE_WIDTH - 2 * GLOBAL_TILE_MARGIN) / 25
for pot_sb in self.p.pot_sbs:
(x_c, y_c) = self.get_sb_box_cent(pot_sb)
if (dx - x_c) * (dx - x_c) + (dy - y_c) * (dy - y_c) <= pw * pw:
# print(type(pot_sb), pot_sb)
self.p.add_sb(pot_sb)
return True
return False
def scene_MousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton: # 左键按下
self.preMousePosition = event.scenePos() # 获取鼠标当前位置
# print(self.preMousePosition.x(), self.preMousePosition.y(), self.graphicsView.size())
tile_loc = self.get_tile(self.preMousePosition)
is_tile = tile_loc[0] >= 0
is_working_tile = tile_loc in self.p.design.th
s_tile = PICK_TILE(tile_loc, is_tile, is_working_tile)
if self.p.edit_mode and self.p.change_placement:
self.target_tile = s_tile
else:
self.selected_tile = s_tile
hl_segs = []
if is_tile and is_working_tile:
for th_node in self.p.design.th[tile_loc]:
if th_node.width == self.p.design_width:
for net in th_node.nets:
for seg_id in self.p.design.net_to_segs[net]:
s = self.p.design.sg[seg_id]
hl_segs.append(s)
self.img_draw.select_tile(tile_loc, hl_segs)
self.show_selected_tile()
# try to add sb
if self.p.edit_mode and not self.p.change_placement:
if self.check_sb(self.preMousePosition):
return
if event.button() == QtCore.Qt.RightButton: # 右键按下
print("鼠标右键单击") # 响应测试语句
self.refresh_img()
def scene_mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
self.MouseMove = event.scenePos() - self.preMousePosition # 鼠标当前位置-先前位置=单次偏移量
self.preMousePosition = event.scenePos() # 更新当前鼠标在窗口上的位置,下次移动用
self.pixmapItem.setPos(self.pixmapItem.pos() + self.MouseMove) # 更新图元位置
# 定义滚轮方法。当鼠标在图元范围之外,以图元中心为缩放原点;当鼠标在图元之中,以鼠标悬停位置为缩放中心
def scene_wheelEvent(self, event):
angle = event.delta() / 8 # 返回QPoint对象,为滚轮转过的数值,单位为1/8度
if angle > 0:
self.ratio += self.zoom_step # 缩放比例自加
if self.ratio > self.zoom_max:
self.ratio = self.zoom_max
else:
w = self.pixmap.size().width() * (self.ratio - self.zoom_step)
h = self.pixmap.size().height() * (self.ratio - self.zoom_step)
x1 = self.pixmapItem.pos().x() # 图元左位置
x2 = self.pixmapItem.pos().x() + w # 图元右位置
y1 = self.pixmapItem.pos().y() # 图元上位置
y2 = self.pixmapItem.pos().y() + h # 图元下位置
if event.scenePos().x() > x1 and event.scenePos().x() < x2 \
and event.scenePos().y() > y1 and event.scenePos().y() < y2: # 判断鼠标悬停位置是否在图元中
self.pixmapItem.setScale(self.ratio) # 缩放
a1 = event.scenePos() - self.pixmapItem.pos() # 鼠标与图元左上角的差值
a2=self.ratio/(self.ratio- self.zoom_step)-1 # 对应比例
delta = a1 * a2
self.pixmapItem.setPos(self.pixmapItem.pos() - delta)
else:
self.pixmapItem.setScale(self.ratio) # 缩放
delta_x = (self.pixmap.size().width() * self.zoom_step) / 2 # 图元偏移量
delta_y = (self.pixmap.size().height() * self.zoom_step) / 2
self.pixmapItem.setPos(self.pixmapItem.pos().x() - delta_x,
self.pixmapItem.pos().y() - delta_y) # 图元偏移
else:
self.ratio -= self.zoom_step
if self.ratio < self.zoom_min:
self.ratio = self.zoom_min
else:
w = self.pixmap.size().width() * (self.ratio + self.zoom_step)
h = self.pixmap.size().height() * (self.ratio + self.zoom_step)
x1 = self.pixmapItem.pos().x()
x2 = self.pixmapItem.pos().x() + w
y1 = self.pixmapItem.pos().y()
y2 = self.pixmapItem.pos().y() + h
if event.scenePos().x() > x1 and event.scenePos().x() < x2 \
and event.scenePos().y() > y1 and event.scenePos().y() < y2:
self.pixmapItem.setScale(self.ratio) # 缩放
a1 = event.scenePos() - self.pixmapItem.pos() # 鼠标与图元左上角的差值
a2=self.ratio/(self.ratio+ self.zoom_step)-1 # 对应比例
delta = a1 * a2
self.pixmapItem.setPos(self.pixmapItem.pos() - delta)
else:
self.pixmapItem.setScale(self.ratio)
delta_x = (self.pixmap.size().width() * self.zoom_step) / 2
delta_y = (self.pixmap.size().height() * self.zoom_step) / 2
self.pixmapItem.setPos(self.pixmapItem.pos().x() + delta_x, self.pixmapItem.pos().y() + delta_y)
class GUI(QWidget):
def __init__(self):
super().__init__()
self.ui = QUiLoader().load('edi.ui')
self.graphic=IMG_WIN(self, self.ui.graphicsView) # 实例化IMG_WIN类
self.label = INFO_WIN(self.ui.label)
self.seg_dis = INFO_WIN(self.ui.segment)
self.net_dis = INFO_WIN(self.ui.nets)
self.ui.pushButton.clicked.connect(self.load_file)
self.ui.switchButton.clicked.connect(self.switch_width)
self.ui.setButton.clicked.connect(self.place)
self.ui.editButton.clicked.connect(self.edit)
self.ui.saveButton.clicked.connect(self.save_design)
self.ui.rlButton.clicked.connect(self.reload_file)
self.ui.A_bt.clicked.connect(self.pick_seg)
self.ui.B_bt.clicked.connect(self.rm_last)
self.ui.C_bt.clicked.connect(self.quit_seg)
self.ui.D_bt.clicked.connect(self.set_seg)
self.ui.lineEdit.returnPressed.connect(self.id_input)
self.ui.reg_edit.returnPressed.connect(self.reg_input)
self.rec = 0
self.edit_mode = False
self.change_placement = True
self.highlight_net = False
self.design_width = 1
self.cur_seg = "-1"
self.dirname = ""
################ FILE CODE #################
def load_file(self):
# USE later for loading
filePath, _ = QFileDialog.getOpenFileName(
self.ui,
"Select design files", # 标题
r"E:\picture\test", # 起始目录
"Graphs(*.graph);;Route file(*.route);;All files(*.*)" #"file type (*.graph *.id_to_name *.packed *.place *route)"
)
if len(filePath) == 0:
return
print(filePath)
self.dirname = os.path.dirname(filePath)
self.process_path(self.dirname)
self.ui.lineEdit.clear()
self.seg_display("")
# self.count = self.design.type_count
self.count = 6
(self.dim_x, self.dim_y) = self.design.get_dim()
self.edit_mode = False
self.change_placement = True
self.graphic.load_img(self.dim_x, self.dim_y, self.count)
self.id_input()
self.show_free_nets()
return
def reload_file(self):
self.save_design()
self.process_path(self.dirname)
return
def process_path(self, filePath):
dirname = filePath
netlist = os.path.join(dirname, "design.packed")
assert os.path.exists(netlist), netlist + " does not exist"
placement = os.path.join(dirname, "design.place")
assert os.path.exists(placement), placement + " does not exists"
route = os.path.join(dirname, "design.route")
assert os.path.exists(route), route + " does not exists"
id_to_name_filename = os.path.join(dirname, "design.id_to_name")
assert os.path.exists(id_to_name_filename), id_to_name_filename + " does not exists"
graph1 = os.path.join(dirname, "1.graph")
assert os.path.exists(graph1), route + " does not exists"
graph16 = os.path.join(dirname, "16.graph")
if not os.path.exists(graph16):
graph16 = os.path.join(dirname, "17.graph")
assert os.path.exists(graph16), route + " does not exists"
self.design = Design(netlist, placement, route, id_to_name_filename, [graph1, graph16])
#enable sta
clk_info, s, e = self.design.sta()
sta_info = clk_info + "\n" + s + "\n" + e
self.display_message("MSG:", sta_info)
return
def save_design(self):
if self.dirname == "":
self.display_message("WARN:", "Please load a design\n")
if self.edit_mode:
self.display_message("WARN:", "Please finish editing before saving\n")
for seg_id in self.design.sg:
if not self.design.sg[seg_id].settled:
self.display_message("WARN:", "Please finish editing before saving\n")
self.save_place()
self.save_route()
self.display_message("MSG", "SUCCESS")
# self.process_path(self.dirname)
return
def save_place(self):
place_f = os.path.join(self.dirname, "design.place")
assert (os.path.exists(place_f))
content = ""
print(self.design.result_graph.added_regs)
print(self.design.result_graph.start_regs)
f = open(place_f, "r")
lines = f.readlines()
line_ind = 0
while line_ind < len(lines):
if line_ind < 2:
content += lines[line_ind]
line_ind += 1
else:
line = lines[line_ind].strip()
line_ind += 1
tokens = line.split("\t")
t_id = tokens[-1][1:]
if t_id in self.design.tr_map:
node = self.design.tr_map[t_id][0]
tokens[2] = str(node.x)
tokens[3] = str(node.y)
content += tokens[0] + "\t\t" + tokens[2] + "\t" + tokens[3] + "\t\t" + tokens[-1] + "\n"
else:
content += line + "\n"
f.close()
f = open(place_f, "w")
f.write(content)
f.close()
return
def save_route(self):
route_f = os.path.join(self.dirname, "design.route")
assert (os.path.exists(route_f))
f = open(route_f, 'w')
content = ""
for net_id in self.design.net_to_segs: #TODO
record_r = set()
segments = self.design.net_to_segs[net_id]
content += "Net ID: "+ net_id + " Segment Size: " + str(len(segments)) + "\n"
i = 0
for seg_id in segments:
seg = self.design.sg[seg_id].nodes
content += "Segment: " + str(i) + " Size: " + str(len(seg)) + "\n"
for node in seg:
n = ""
if node.route_type == RouteType.SB:
n = "SB (" + str(node.track) + ", " + str(node.x) + ", " + str(node.y) + ", " + str(node.side) + ", " + str(node.io) + ", " + str(node.bit_width) + ")\n"
elif node.route_type == RouteType.PORT:
n = "PORT " + node.port + " (" + str(node.x) + ", " + str(node.y) + ", " + str(node.bit_width) + ")\n"
elif node.route_type == RouteType.RMUX:
n = "RMUX " + node.rmux_name + " (" + str(node.x) + ", " + str(node.y) + ", " + str(node.bit_width) + ")\n"
else:
n = "REG " + node.reg_name + " (" + str(node.track) + ", " + str(node.x) + ", " + str(node.y) + ", " + str(node.bit_width) + ")\n"
content += n
i += 1
content += "\n"
f.write(content)
f.close()
return
################ IMAGE CODE #################
def reload_img(self):
# self.graphic.load_img(self.dim_x, self.dim_y, self.count)
self.graphic.reload_img()
return
def refresh_img(self):
self.graphic.refresh_img()
return
def switch_width(self):
if self.design_width == 1:
self.design_width = 17
else:
self.design_width = 1
self.graphic.reload_img()
self.graphic.show_selected_tile()
return
def highlight(self):
if self.highlight_net:
self.highlight_net = False
else:
self.highlight_net = True
self.display_message("Check:", "do nothing\n")
return
################ TILE CODE #################
def edit(self):
if self.edit_mode:
self.display_message("WARN:", "Please load design first")
return
if not self.can_edit_tile():
return
self.change_placement = True
self.edit_mode = True
# self.display_text("In Edit Mode")
self.graphic.show_selected_tile()
return
def can_edit_tile(self):
p_tile = self.graphic.selected_tile
if (not p_tile.is_tile) or (not p_tile.is_working_tile):
return False
# if p_tile.loc[1] == 0: #cannot edit port tile
# return False
return True
def place(self):
if not self.edit_mode and not self.change_placement:
return
# no edit
if self.graphic.selected_tile.loc == self.graphic.target_tile.loc:
self.edit_mode = False
self.graphic.show_selected_tile()
return
if not self.try_place():
return
self.place_tile()
# self.display_text("Exit Edit Mode")
self.graphic.show_selected_tile()
return
def place_tile(self):
print("succeed!!!")
self.edit_mode = False
assert(self.graphic.selected_tile.loc in self.design.th)
assert(self.graphic.target_tile.loc not in self.design.th)
loc_ori = self.graphic.selected_tile.loc
loc_new = self.graphic.target_tile.loc
# update th
val = self.design.th[self.graphic.selected_tile.loc]
del self.design.th[self.graphic.selected_tile.loc]
self.graphic.selected_tile = self.graphic.target_tile
self.graphic.target_tile = self.graphic.empty_tile
loc = self.graphic.selected_tile.loc
key = (int(loc[0]), int(loc[1]))
self.design.th[key] = val
# update sg
for th_n in val:
for net_id in th_n.nets:
for seg_id in self.design.net_to_segs[net_id]:
s = self.design.sg[seg_id]
if s.settled:
s.settled = False
# update rh
for n in s.nodes:
if n.route_type == RouteType.SB and n in self.design.rh[(n.x, n.y)]:
self.design.rh[(n.x, n.y)].remove(n)
del s.nodes[1:-1]
# update tn
t_n = self.design.blk_id_list[th_n.id]
t_n.x = int(loc[0])
t_n.y = int(loc[1])
# update tr_map ?
routes = self.design.tr_map[th_n.id]
for r in routes:
r.x = int(loc[0])
r.y = int(loc[1])
self.id_input()
self.show_free_nets()
self.reload_img()
return
def try_place(self):
s_tile = self.graphic.selected_tile
e_tile = self.graphic.target_tile
if e_tile.is_working_tile or (not e_tile.is_tile):
return False
#some restrictions, may need further discussion
source_type = [th_n.ty for th_n in self.design.th[s_tile.loc]]
lim_type = TileType.PE
if e_tile.loc[1] == 0:
lim_type = TileType.IO1
elif e_tile.loc[0] % 4 == 3:
lim_type = TileType.MEM
if lim_type == TileType.PE:
if TileType.MEM in source_type:
return False
else:
return True
if lim_type == TileType.MEM:
if TileType.PE in source_type:
return False
else:
return True
if lim_type == TileType.IO1:
if TileType.IO1 in source_type or TileType.IO16 in source_type:
return True
else:
return False
def detector(self):
self.label.display_text("detected: " + str(self.rec))
self.rec += 1
return
def show_free_nets(self):
s = "Free Nets: \n========\n"
for seg_id in self.design.sg:
if not self.design.sg[seg_id].settled:
seg = self.design.sg[seg_id]
s += "segment " + str(seg_id) + " in net " + str(seg.net) + " " + str(seg.width) + "\n"
self.nets_display(s)
def valid_reg_name(self, name):
if len(name) < 3:
return False
if name[0] != "T":
return False
if not name[1].isdigit():
return False
if name[2] != "_":
return False
return True
def reg_input(self):
if not self.graphic.started:
self.display_message("WARN:", "Please load design first")
return
if self.edit_mode:
self.display_message("WARN:", "Please finish the current editing")
return
s = self.ui.reg_edit.text()
if s == "Reg Edit" or s == "":
return
#input format: reg_id x y (reg_name)
#or: reg_id reg_name
r_input = s.split(" ")
if r_input[0] not in self.design.blk_id_list:
self.display_message("WARN:", "Invalid REG ID")
return
pre_x = self.design.blk_id_list[r_input[0]].x
pre_y = self.design.blk_id_list[r_input[0]].y
wid = self.design.tr_map[r_input[0]][0].bit_width
x = -1
y = -1
n_reg_name = ""
valid_input = True
if len(r_input) == 2:
if self.valid_reg_name(r_input[1]):
n_reg_name = r_input[1]
x = pre_x
y = pre_y
else:
valid_input = False
elif len(r_input) == 3:
x = int(r_input[1])
y = int(r_input[2])
n_reg_name = self.design.tr_map[r_input[0]][0].reg_name
elif len(r_input) == 4:
x = int(r_input[1])
y = int(r_input[2])
if self.valid_reg_name(r_input[3]):
n_reg_name = r_input[3]
else:
valid_input = False
if not valid_input:
self.display_message("WARN:", "Invalid input")
return
# if len(r_input) == 3:
# r_input.append(self.design.tr_map[r_input[0]][0].reg_name)
# n_reg_name = r_input[3]
if x < 0 or x >= self.dim_x or y < 1 or y >= self.dim_y:
self.display_message("WARN:", "Invalid location")
return
s_check = False
for th_n in self.design.th[(pre_x, pre_y)]:
if th_n.id == r_input[0]:
s_check = True
break
assert(s_check)
#check if the new location is occupied
#occupied if there is a REG at the new location
free = True
if (x, y) in self.design.th:
for th_n in self.design.th[(x, y)]:
if th_n.ty == TileType.REG:
t_wid = th_n.width
if self.design.tr_map[th_n.id][0].reg_name == n_reg_name and t_wid == wid:
free = False
break
if not free:
self.display_message("WARN:", "The new location is occupied")
return
#occupied if there is a seg at the new location
new_track = int(n_reg_name[1])
if new_track < 0 or new_track > 4:
self.display_message("WARN:", "Invalid track")
return
dir = n_reg_name[3:]
new_dir = -1
if dir == "NORTH":
new_dir = 3
elif dir == "SOUTH":
new_dir = 1
elif dir == "EAST":
new_dir = 0
elif dir == "WEST":
new_dir = 2
if new_dir == -1:
self.display_message("WARN:", "Invalid direction")
return
net_ids = set()
for r_n in self.design.tr_map[r_input[0]]:
net_ids.add(r_n.net_id)
if (x, y) in self.design.rh:
for r_n in self.design.rh[(x, y)]:
if r_n.route_type == RouteType.SB and r_n.io == 1:
if r_n.track == new_track and r_n.side == new_dir and r_n.bit_width == wid and r_n.net_id not in net_ids:
free = False
break
if not free:
self.display_message("WARN:", "The new location is occupied")
return
#finish checking, now we can move the REG
self.design.blk_id_list[r_input[0]].x = x
self.design.blk_id_list[r_input[0]].y = y
for r_n in self.design.tr_map[r_input[0]]:
r_n.x = x
r_n.y = y
r_n.track = new_track
r_n.reg_name = n_reg_name
ind = 0
while ind < len(self.design.th[(pre_x, pre_y)]):
if self.design.th[(pre_x, pre_y)][ind].id == r_input[0]:
break
ind += 1
th_n = self.design.th[(pre_x, pre_y)].pop(ind)
if len(self.design.th[(pre_x, pre_y)]) == 0:
del self.design.th[(pre_x, pre_y)]
if (x, y) not in self.design.th:
self.design.th[(x, y)] = []
self.design.th[(x, y)].append(th_n)
for net_id in th_n.nets:
for seg_id in self.design.net_to_segs[net_id]:
sg = self.design.sg[seg_id]
if sg.settled:
sg.settled = False
# update rh