#!/usr/bin/python import sys,os import idl (TYPE_DEFBOOL, TYPE_BOOL, TYPE_INT, TYPE_UINT, TYPE_STRING, TYPE_ARRAY, TYPE_AGGREGATE) = range(7) def py_type(ty): if ty == idl.bool: return TYPE_BOOL if ty.typename == "libxl_defbool": return TYPE_DEFBOOL if isinstance(ty, idl.Enumeration): return TYPE_UINT if isinstance(ty, idl.Number): if ty.signed: return TYPE_INT else: return TYPE_UINT if isinstance(ty, idl.Array): return TYPE_ARRAY if isinstance(ty, idl.Aggregate): return TYPE_AGGREGATE if ty == idl.string: return TYPE_STRING return None def py_wrapstruct(ty): l = [] l.append('typedef struct {') l.append(' PyObject_HEAD;') l.append(' %s obj;'%ty.typename); l.append('}Py_%s;'%ty.rawname) l.append('') return "\n".join(l) + "\n" def fsanitize(name): "Sanitise a function name given a C type" ret = '_'.join(name.split()) return ret.replace('*', 'ptr') def py_decls(ty): l = [] if isinstance(ty, idl.Aggregate): l.append('_hidden Py_%s *Py%s_New(void);\n'%(ty.rawname, ty.rawname)) l.append('_hidden int Py%s_Check(PyObject *self);\n'%ty.rawname) for f in ty.fields: if py_type(f.type) is not None: continue if py_type(f.type) == TYPE_DEFBOOL: continue if ty.marshal_out(): l.append('_hidden PyObject *attrib__%s_get(%s *%s);'%(\ fsanitize(f.type.typename), f.type.typename, f.name)) if ty.marshal_in(): l.append('_hidden int attrib__%s_set(PyObject *v, %s *%s);'%(\ fsanitize(f.type.typename), f.type.typename, f.name)) return '\n'.join(l) + "\n" def py_attrib_get(ty, f): t = py_type(f.type) l = [] l.append('static PyObject *py_%s_%s_get(Py_%s *self, void *priv)'%(ty.rawname, f.name, ty.rawname)) l.append('{') if t == TYPE_BOOL: l.append(' PyObject *ret;') l.append(' ret = (self->obj.%s) ? Py_True : Py_False;'%f.name) l.append(' Py_INCREF(ret);') l.append(' return ret;') elif t == TYPE_DEFBOOL: l.append(' return genwrap__defbool_get(&self->obj.%s);'%f.name) elif t == TYPE_INT: l.append(' return genwrap__ll_get(self->obj.%s);'%f.name) elif t == TYPE_UINT: l.append(' return genwrap__ull_get(self->obj.%s);'%f.name) elif t == TYPE_STRING: l.append(' return genwrap__string_get(&self->obj.%s);'%f.name) elif t == TYPE_AGGREGATE or t == TYPE_ARRAY: l.append(' PyErr_SetString(PyExc_NotImplementedError, "Getting %s");'%ty.typename) l.append(' return NULL;') else: tn = f.type.typename l.append(' return attrib__%s_get((%s *)&self->obj.%s);'%(fsanitize(tn), tn, f.name)) l.append('}') return '\n'.join(l) + "\n\n" def py_attrib_set(ty, f): t = py_type(f.type) l = [] l.append('static int py_%s_%s_set(Py_%s *self, PyObject *v, void *priv)'%(ty.rawname, f.name, ty.rawname)) l.append('{') if t == TYPE_BOOL: l.append(' self->obj.%s = (NULL == v || Py_None == v || Py_False == v) ? 0 : 1;'%f.name) l.append(' return 0;') elif t == TYPE_DEFBOOL: l.append(' return genwrap__defbool_set(v, &self->obj.%s);'%f.name) elif t == TYPE_UINT or t == TYPE_INT: l.append(' %slong long tmp;'%(t == TYPE_UINT and 'unsigned ' or '')) l.append(' int ret;') if t == TYPE_UINT: l.append(' ret = genwrap__ull_set(v, &tmp, (%s)~0);'%f.type.typename) else: l.append(' ret = genwrap__ll_set(v, &tmp, (%s)~0);'%f.type.typename) l.append(' if ( ret >= 0 )') l.append(' self->obj.%s = tmp;'%f.name) l.append(' return ret;') elif t == TYPE_STRING:
/*
Example from: https://www.latticesemi.com/-/media/LatticeSemi/Documents/UserManuals/EI/iCEcube201701UserGuide.ashx?document_id=52071 [p. 72].
*/
module top (din, write_en, waddr, wclk, raddr, rclk, dout);
parameter addr_width = 8;
parameter data_width = 8;
input [addr_width-1:0] waddr, raddr;
input [data_width-1:0] din;
input write_en, wclk, rclk;
output [data_width-1:0] dout;
reg [data_width-1:0] dout;
reg [data_width-1:0] mem [(1<<addr_width)-1:0]
/* synthesis syn_ramstyle = "no_rw_check" */ ;
always @(posedge wclk) // Write memory.
begin
if (write_en)
mem[waddr] <= din; // Using write address bus.
end
always @(posedge rclk) // Read memory.
begin
dout <= mem[raddr]; // Using read address bus.
end
endmodule