@@ -18,3 +18,86 @@ def from_attributes(obj):
18
18
if hasattr (x , "get_fragment" ):
19
19
f += x .get_fragment ()
20
20
return f
21
+
22
+ class _FModuleProxy :
23
+ def __init__ (self , fm ):
24
+ object .__setattr__ (self , "_fm" , fm )
25
+
26
+ class _FModuleComb (_FModuleProxy ):
27
+ def __iadd__ (self , other ):
28
+ if isinstance (other , (list , tuple )):
29
+ self ._fm ._fragment .comb += other
30
+ else :
31
+ self ._fm ._fragment .comb .append (other )
32
+ return self
33
+
34
+ def _cd_append (d , key , statements ):
35
+ try :
36
+ l = d [key ]
37
+ except KeyError :
38
+ l = []
39
+ d [key ] = l
40
+ if isinstance (statements , (list , tuple )):
41
+ l += other
42
+ else :
43
+ l .append (statements )
44
+
45
+ class _FModuleSyncCD :
46
+ def __init__ (self , fm , cd ):
47
+ self ._fm = fm
48
+ self ._cd = cd
49
+
50
+ def __iadd__ (self , other ):
51
+ _cd_append (self ._fm ._fragment .sync , self ._cd , other )
52
+ return self
53
+
54
+ class _FModuleSync (_FModuleProxy ):
55
+ def __iadd__ (self , other ):
56
+ _cd_append (self ._fm ._fragment .sync , "sys" , other )
57
+ return self
58
+
59
+ def __getattr__ (self , name ):
60
+ return _FModuleSyncCD (self ._fm , name )
61
+
62
+ def __setattr__ (self , name , value ):
63
+ if not isinstance (value , _FModuleSyncCD ):
64
+ raise AttributeError ("Attempted to assign sync property - use += instead" )
65
+
66
+ class _FModuleSpecials (_FModuleProxy ):
67
+ def __iadd__ (self , other ):
68
+ if isinstance (other , (set , list , tuple )):
69
+ self ._fm ._fragment .specials |= set (other )
70
+ else :
71
+ self ._fm ._fragment .specials .add (other )
72
+ return self
73
+
74
+ class FModule :
75
+ def do_simulation (self , s ):
76
+ pass
77
+
78
+ def get_fragment (self ):
79
+ assert (not hasattr (self , "_fragment" ))
80
+ self ._fragment = Fragment (sim = [self .do_simulation ])
81
+ self .build_fragment ()
82
+ self ._fragment += from_attributes (self )
83
+ return self ._fragment
84
+
85
+ def __getattr__ (self , name ):
86
+ if name == "comb" :
87
+ return _FModuleComb (self )
88
+ elif name == "sync" :
89
+ return _FModuleSync (self )
90
+ elif name == "specials" :
91
+ return _FModuleSpecials (self )
92
+ else :
93
+ raise AttributeError
94
+
95
+ def __setattr__ (self , name , value ):
96
+ if name in ["comb" , "sync" , "specials" ]:
97
+ if not isinstance (value , _FModuleProxy ):
98
+ raise AttributeError ("Attempted to assign special FModule property - use += instead" )
99
+ else :
100
+ object .__setattr__ (self , name , value )
101
+
102
+ def build_fragment (self ):
103
+ raise NotImplementedError ("FModule.build_fragment needs to be overloaded" )
0 commit comments