Created
June 7, 2016 08:27
-
-
Save shtaxxx/d68719fd67be656f41137d39b9d2d7cf to your computer and use it in GitHub Desktop.
Get I/O port by veriloggen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import print_function | |
import sys | |
import os | |
import collections | |
## modified version of veriloggen/examples/read_verilog_code/read_verilog_code.py | |
# the next line can be removed after installation | |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | |
from veriloggen import * | |
led_v = '''\ | |
module blinkled # | |
( | |
parameter WIDTH = 8 | |
) | |
( | |
input CLK, | |
input RST, | |
output reg [WIDTH-1:0] LED | |
); | |
reg [32-1:0] count; | |
always @(posedge CLK) begin | |
if(RST) begin | |
count <= 0; | |
end else begin | |
if(count == 1023) begin | |
count <= 0; | |
end else begin | |
count <= count + 1; | |
end | |
end | |
end | |
always @(posedge CLK) begin | |
if(RST) begin | |
LED <= 0; | |
end else begin | |
if(count == 1023) begin | |
LED <= LED + 1; | |
end | |
end | |
end | |
endmodule | |
''' | |
def mkLed(): | |
modules = from_verilog.read_verilog_module_str(led_v) | |
m = modules['blinkled'] | |
ports = m.get_ports() | |
for name, port in ports.items(): | |
print(name, type(port), port) | |
# change the module name | |
m.name = 'modified_led' | |
# add new statements | |
enable = m.Input('enable') | |
busy = m.Output('busy') | |
old_statement = m.always[0].statement[0].false_statement | |
m.always[0].statement[0].false_statement = If(enable)(*old_statement) | |
m.Assign( busy(m.variable['count'] < 1023) ) | |
return m | |
if __name__ == '__main__': | |
led = mkLed() | |
verilog = led.to_verilog() | |
print(verilog) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import print_function | |
import sys | |
import os | |
import collections | |
## modified version of veriloggen/tests/verilog/read_verilog_/module/read_verilog_module.py | |
# the next line can be removed after installation | |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))) | |
from veriloggen import * | |
def mkLed(): | |
filename = os.path.dirname(os.path.abspath(__file__)) + '/led.v' | |
modules = from_verilog.read_verilog_module(filename) | |
m = modules['blinkled'] | |
ports = m.get_ports() | |
for name, port in ports.items(): | |
print(name, type(port), port) | |
return m | |
def mkTop(): | |
m = Module('top') | |
width = m.Parameter('WIDTH', 8) | |
clk = m.Input('CLK') | |
rst = m.Input('RST') | |
led = m.Output('LED', width) | |
params = ( width, ) | |
ports = ( clk, rst, led ) | |
led = mkLed() | |
m.Instance(led, 'inst_blinkled', params, ports) | |
return m | |
if __name__ == '__main__': | |
top = mkTop() | |
verilog = top.to_verilog() | |
print(verilog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment