44import sys
55
66from amaranth .lib import wiring
7+ from amaranth_soc .memory import MemoryMap
78from docstring_parser import parse_from_object
89
10+
911import chipflow_digital_ip
1012
1113def children (mod ):
@@ -23,16 +25,58 @@ def children(mod):
2325 done .append (e )
2426 stack .extend ([(f"{ e } .{ attr } " , getattr (o , attr )) for attr in dir (o ) if not attr .startswith ("_" )])
2527
28+ def name_to_str (n : MemoryMap .Name ) -> str :
29+ return "." .join (f"{ part } " for part in n )
30+
31+ def docs_for_obj (obj ) -> dict :
32+ docstring = parse_from_object (obj )
33+ #init_signature = inspect.signature(cls.__init__)
34+ #print(f"{name} Signature:i {init_signature}")
35+ d = {}
36+ if docstring .short_description :
37+ d ['short_description' ] = docstring .short_description
38+ if docstring .long_description :
39+ d ['long_description' ] = docstring .long_description
40+ if docstring .params :
41+ d ['params' ] = [p .__dict__ for p in docstring .params if p .args [0 ]== "param" ]
42+ d ['attributes' ] = [p .__dict__ for p in docstring .params if p .args [0 ]== "attribute" ]
43+ if docstring .raises :
44+ d ['raises' ] = [p .__dict__ for p in docstring .raises ]
45+ if docstring .examples :
46+ d ['examples' ] = [ e .__dict__ for e in docstring .examples ]
47+ return d
48+
49+
50+ def list_resources (mm : MemoryMap ) -> dict :
51+ d = {}
52+ for resource , rname , _ in mm .resources ():
53+ cls = resource .__class__
54+ print (f"adding resource { resource } . { type (resource )} { cls .__name__ } , { cls .__qualname__ } , { cls .__doc__ } " )
55+ ri = mm .find_resource (resource )
56+ d [name_to_str (rname )] = {
57+ 'resource' : docs_for_obj (resource ),
58+ 'start' : f"{ ri .start :#x} " ,
59+ 'end' : f"{ ri .end :#x} " ,
60+ 'width' : ri .width ,
61+ }
62+ for window , wname , (start , end , ratio ) in mm .windows ():
63+ d [name_to_str (wname )] = {
64+ 'window' : docs_for_obj (window ),
65+ 'start' : f"{ start :#x} " ,
66+ 'end' : f"{ end :#x} " ,
67+ 'ratio' : ratio ,
68+ 'children' : list_resources (window )
69+ }
70+ return d
71+
2672gen = children (chipflow_digital_ip )
2773output = {}
2874for name , cls in gen :
29- docstring = parse_from_object (cls )
30- d = {
31- 'short_description' : docstring .short_description ,
32- 'long_description' : docstring .long_description ,
33- 'params' : [p .__dict__ for p in docstring .params ],
34- 'examples' : [ e .__dict__ for e in docstring .examples ],
35- }
75+ # instantiate and get registers
76+ obj = cls ()
77+ d = docs_for_obj (obj )
78+ if hasattr (obj , 'bus' ) and hasattr (obj .bus , 'memory_map' ) and isinstance (obj .bus .memory_map , MemoryMap ):
79+ d ['memory_map' ] = list_resources (obj .bus .memory_map )
3680 output [name ] = d
3781
3882json .dump (output , sys .stdout , indent = 2 )
0 commit comments