In [1]:
import magma as m
In [2]:
HalfAdder = m.DefineCircuit('HalfAdder',
'A', m.In(m.Bit),
'B', m.In(m.Bit),
'S', m.Out(m.Bit),
'C', m.Out(m.Bit))
HalfAdder.verilog = '''\
assign S = A ^ B;
assign C = A & B;\
'''
m.EndCircuit()
In [3]:
print(HalfAdder)
In [4]:
verilog_source = '''
module _HalfAdder(a, b, c);
input a;
output b;
inout c;
assign a = b & c;
endmodule'''
HalfAdder = m.DefineFromVerilog(verilog_source)[0]
print(HalfAdder)
The functions:
DeclareFromVerilogFile(filename)
DefineFromVerilogFile(filename)
can be used to declare and define Magma
circuits from verilog files.
The declare versions declare a Magma
circuit, but does not include the verilog source code.
The define versions define a Magma
circuit which includes the verilog source code.
In [5]:
mako_source = '''module CSA${N} ( input [${N-1}:0] a,b,c, output [${N-1}:0] s, co );
assign s = a ^ b ^c;
assign co = a&b | b&c | a&c;
endmodule'''
In [6]:
from mako.template import Template
template = Template(mako_source)
verilog = template.render(N=4)
print(verilog)
The functions:
DeclareFromTemplatedVerilogFile(filename, **args)
DefineFromTemplatedVerilogFile(filename, **args)
can be used to declare and define Magma
circuits from templated verilog files.
The keyword args are passed to the templating engine
and can be used to control the generation of verilog.
In [7]:
CSA4 = m.DefineFromTemplatedVerilog(mako_source, N=4)[0]
print(CSA4)
See the repo https://github.com/phanrahan/genesis3 for a simple verilog generator similar to Genesis2.
In [ ]: