4 def derive(module
, parameters
):
5 assert module
== r
"python_inv"
6 if parameters
.keys() != {r
"\width"}:
7 raise ValueError("Invalid parameters")
10 wire width {width:d} input 1 \i
11 wire width {width:d} output 2 \o
13 parameter \A_SIGNED 1'0
14 parameter \A_WIDTH 32'{width:b}
15 parameter \Y_WIDTH 32'{width:b}
21 wire width {width:d} input 1 \i
22 wire width {width:d} output 2 \o
28 """.format(width
=parameters
[r
"\width"])
30 # ----------------------------------------------------------------------------
34 import sys
, socket
, os
, subprocess
36 import msvcrt
, win32pipe
, win32file
38 msvcrt
= win32pipe
= win32file
= None
40 def map_parameter(parameter
):
41 if parameter
["type"] == "unsigned":
42 return int(parameter
["value"], 2)
43 if parameter
["type"] == "signed":
44 width
= len(parameter
["value"])
45 value
= int(parameter
["value"], 2)
46 if value
& (1 << (width
- 1)):
47 value
= -((1 << width
) - value
)
49 if parameter
["type"] == "string":
50 return parameter
["value"]
51 if parameter
["type"] == "real":
52 return float(parameter
["value"])
55 input = json
.loads(input_json
)
56 if input["method"] == "modules":
57 return json
.dumps({"modules": modules()})
58 if input["method"] == "derive":
60 frontend
, source
= derive(input["module"],
61 {name
: map_parameter(value
) for name
, value
in input["parameters"].items()})
62 return json
.dumps({"frontend": frontend
, "source": source
})
63 except ValueError as e
:
64 return json
.dumps({"error": str(e
)})
67 parser
= argparse
.ArgumentParser()
68 modes
= parser
.add_subparsers(dest
="mode")
69 mode_stdio
= modes
.add_parser("stdio")
70 if os
.name
== "posix":
71 mode_path
= modes
.add_parser("unix-socket")
73 mode_path
= modes
.add_parser("named-pipe")
74 mode_path
.add_argument("path")
75 args
= parser
.parse_args()
77 if args
.mode
== "stdio":
79 input = sys
.stdin
.readline()
81 sys
.stdout
.write(call(input) + "\n")
84 if args
.mode
== "unix-socket":
85 sock
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
90 ys_proc
= subprocess
.Popen(["../../yosys", "-ql", "unix.log", "-p", "connect_rpc -path {}; read_verilog design.v; hierarchy -top top; flatten; select -assert-count 1 t:$neg".format(args
.path
)])
91 conn
, addr
= sock
.accept()
92 file = conn
.makefile("rw")
94 input = file.readline()
96 file.write(call(input) + "\n")
98 ys_proc
.wait(timeout
=10)
99 if ys_proc
.returncode
:
100 raise subprocess
.CalledProcessError(ys_proc
.returncode
, ys_proc
.args
)
106 if args
.mode
== "named-pipe":
107 pipe
= win32pipe
.CreateNamedPipe(args
.path
, win32pipe
.PIPE_ACCESS_DUPLEX
,
108 win32pipe
.PIPE_TYPE_BYTE|win32pipe
.PIPE_READMODE_BYTE|win32pipe
.PIPE_WAIT
,
109 1, 4096, 4096, 0, None)
110 win32pipe
.ConnectNamedPipe(pipe
, None)
114 while not input.endswith(b
"\n"):
115 result
, data
= win32file
.ReadFile(pipe
, 4096)
118 assert not b
"\n" in input or input.endswith(b
"\n")
119 output
= (call(input.decode("utf-8")) + "\n").encode("utf-8")
122 result
, done
= win32file
.WriteFile(pipe
, output
)
125 except win32file
.error
as e
:
126 if e
.args
[0] == 109: # ERROR_BROKEN_PIPE
131 if __name__
== "__main__":