Suppose that we got a verilog file adder.v
for a
half-adder like
module halfadder (a, b, sum, carry);
input a, b;
output sum, carry;
wire sum, carry;
xor(sum, a, b);
and(carry, a, b);
endmodule
Create a directory named mypass/
under the
passes/
directory and create a file mypass.cc
with the following:
#include "kernel/yosys.h"
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
struct MyPass : public Pass {
() : Pass("MyPass","counter") { }
MyPass
void execute(vector<string>, RTLIL::Design *design) override
{
("Inside the newly created pass\n");
log(design, "Executing MyPass\n");
log_headerfor (auto mod : design->modules())
{
(" %s (%d wires, %d cells)\n", log_id(mod),
log(mod->wires()), GetSize(mod->cells()));
GetSize("dump",*&design);
run_pass}
}
} MyPass;
PRIVATE_NAMESPACE_END
We convert behavioural verilog code into gate-level netlist here.
Create a file named script.ys (just a random name) with the following yosys commands:
# Read verilog source and convert to an internal representation
read_verilog adder.v
# Elaborate design hierarchy??
hierarchy -check -top adder
# Convert high-level behavioural constructs (processes) to D-flip flops and MUXes
proc
# Analyze and optimize FSMs
fsm
# Optimizations
opt
# Analyze memories and create their circuit implementations
memory
# Optimizations
opt
# Convert design to logical gate-level netlists (like adders to logic gates)
techmap
# Optimizations
opt
# Map registers to the hardware flip flops available from the cell library
dfflibmap -liberty examples/cmos/cmos_cells.lib
# Map remaining logic to cell library cells
abc -liberty examples/cmos/cmos_cells.lib
# Cleanup (just last step of opt)
clean
# Write results to output file
write_verilog synth.v
and run them with yosys using
yosys -s script.ys
This will create a synth.v
file with the netlist in
it.
We are using a cell library named cmos_cells.lib
from
the examples/
directory of the yosys source code.
Use
yosys-config --build hello.so hello.cc
and a file named hello.so
would get created.
Now use the synth.v
file that got generated earlier and
run
yosys -ql hello.log -m hello.so synth.v -p MyPass
to get the log data in the file hello.log
.