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
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
)
89 conn
, addr
= sock
.accept()
90 file = conn
.makefile("rw")
92 input = file.readline()
94 file.write(call(input) + "\n")
100 if args
.mode
== "named-pipe":
101 pipe
= win32pipe
.CreateNamedPipe(args
.path
, win32pipe
.PIPE_ACCESS_DUPLEX
,
102 win32pipe
.PIPE_TYPE_BYTE|win32pipe
.PIPE_READMODE_BYTE|win32pipe
.PIPE_WAIT
,
103 1, 4096, 4096, 0, None)
104 win32pipe
.ConnectNamedPipe(pipe
, None)
108 while not input.endswith(b
"\n"):
109 result
, data
= win32file
.ReadFile(pipe
, 4096)
112 assert not b
"\n" in input or input.endswith(b
"\n")
113 output
= (call(input.decode("utf-8")) + "\n").encode("utf-8")
116 result
, done
= win32file
.WriteFile(pipe
, output
)
119 except win32file
.error
as e
:
120 if e
.args
[0] == 109: # ERROR_BROKEN_PIPE
125 if __name__
== "__main__":