#######################################################################
# Makefile Template for ADMS VerilogA translation
# For export, and used in examples.
#######################################################################

# This must match the SyntaxLevel defined in device.xml.  If it is
# smaller, you need to update this Makefile to match devkit/Makefile.
SyntaxLevel = 19

#######################################################################
#  MANDATORY User Configuration

# Set this to the path to the Verilog input file.
#
SOURCE = path_to_module.va

# Set this to a unique name for the SPICE model for this device.  This
# name can appear in the .model line:
#   .model myname modelname(level=N ... )
# Devices that reference myname will use our model if the level number
# (below) also matches N in the example.  If level (below) is set to 1,
# level can be omitted from the .model line as it defaults to 1.
#
# Note that this device will also be available using the standard
# SPICE model names and the level given below.  For example, for a
# MOSFET the standard model names are nmos and pmos.  In this case,
# level (below) should be unique among the levels for the device.
#
MODELNAME = modelname

# Set this to a short name word which will be used as the base name of
# the loadable module file.  It is obviously desirable that the name
# be unique among the user's modules.
#
MODULE = name_of_module

# Set this to a single, lower-case alphabetic character which is the
# SPICE key for this device type, e.g., m for MOS, q for BJT.  See the
# device.xml file for a listing.  Use the "real" device key and not
# 'x'.
#
KEY = q

# Set this to the model level number (a positive number) which will
# associate to this device.  When the model name is the standard name
# for the device (e.g., npn or pnp for BJT, nmos or pmos for MOSFET). 
# This should be uniqe among the internal and other loaded devices of
# the same type key letter.  Giving this number in a "level" model
# parameter in WRspice input will signal use of this device.
#
LEVEL = 2

# If this is set to "yes", then if the device does not have an
# instance parameter named "m", a real-valued parameter of this name
# will be added.  The default value is 1.0, and the parameter will
# multiply the contributions from the device model, effectively
# scaling the device count.  For example, giving 'm=2' as a device
# instance parameter is equivalent to specifying two identical,
# parallel devices.
#
ADD_M = no

# When set to yes, a linear predictor is used on each device node to
# provide the initial value for a new time step.  Otherwise, the
# initial value is the final value for the previous time step. 
# Semiconductor device models benefit from this.  However, this may
# effect integration accuracy of reactive elements and Josephson
# junctions, where it is not recommended.
#
PREDICTOR = yes

# When set to yes, and the predictor above is also set to yes, a
# limiting algorithm is used on every device pin during transient
# analysis.  This is probably required for convergence of
# semiconductor device models that contain p/n or Schottky junctions. 
# One can turn this off for non-semiconductor devices.
#
LIMITING = yes

# You probably don't need any flags, but these can be set when needed.
# The string consists of words from the list below, separated by '|'
# symbols.  It should be quoted if there are any '|' characters included.
#
# The possible flags are:
# These are all optional, but prevent unsupported analysis cleanly.
# DV_NOAC       Device does not support AC analysis.
# DV_NODCO      Device does not support DCOP analysis.
# DV_NODCT      Device does not support DCT analysis.
# DV_NOTRAN     Device does not support TRAN analysis.
# DV_NONOIS     Device does not support NOISE analysis.
# DV_NOPZ       Device does not support PZ analysis.
# DV_NODIST     Device does not support DIST analysis.
#
# These are obscure.
# DV_JJSTEP     Device has timestep limiting based on the device
#               voltage and Josephson equation, as in the bound_step
#               call in the Josephson junction example.
#
# DV_JJPMDC     Device is Josephson-junction-like and in DC analysis
#               takes the voltage across the device as scaled phase.
#               If the simulator supports this mode,
#               $simparam("dcPhaseScale", 0.0)
#               will return the scale factor (default 1e6), otherwise 0.
#
# Example (from JJ example)
# FLAGS = "DV_JJSTEP | DV_JJPMDC | DV_NODIST"
#
FLAGS = 

# Change this if WRspice is not installed in the default location,
# which is under /usr/local.
#
PREFIX = /usr/local

# Set this to the directory containing the xml conversion source files,
# if different from the default setting given.
#
ADMST = $(PREFIX)/xictools/wrspice/devkit/admst

# Set this to the name of the admsXml translator program, or a full
# path if installed in a location not in your search path.
#
ADMSXML = admsXml

#  Windows Only!
#
# The plug-in must link against the wrspice.dll file, which is found
# in the bin directory of the wrspice distribution area.  Modify this
# if necessary, it is the full path to the directory containing
# wrspice.dll.
#
DLL_LOC = $(PREFIX)/xictools/wrspice/bin

# This should automatically configure the rest of the Makefile to your
# WRspice environment.  If wrspice in not in your search path, or you
# are cross-building, you should set this to the full path of the
# WRspice executable you wish to build modules for.
#
WRSPICE = wrspice

#  End of (intended) User Configuration.
#  You may still have to mess around below if your build environment
#  isn't like mine, likely for non-Linux.
#######################################################################

ifneq ($(WRSPICE),)
VSTR = $(shell $(WRSPICE) --v)
DEVLIB_VERSION = $(word 1, $(VSTR))
OSNAME = $(word 2, $(VSTR))
ARCH = $(word 3, $(VSTR))
endif

# This Makefile originated from this distribution:
#DEVLIB_VERSION = 4.3.19
#OSNAME = LinuxUnknown
#ARCH = x86_64
#CXX = c++
#CC = cc -std=gnu11
#LINKCC = c++
#CFLAGS = -O3 -Wall -W -Wshadow -Wwrite-strings -pthread 
#LSHFLAG = -shared
#DEPEND_PROG = $(CC) -MM -O3 -Wall -W -Wshadow -Wwrite-strings -pthread
#FILTER = 
#PIC_OPT = -fPIC
#SOEXT = so

DATE = Fri Feb 23 14:31:00 UTC 2024

# Advanced users can edit the blocks below if necessary.

ifeq ($(OSNAME),MINGW)

# MINGW x86_64
CXX = /mingw64/bin/g++
CC = /mingw64/bin/gcc
LINKCC = /mingw64/bin/g++ -static-libgcc -static-libstdc++
CFLAGS = -O2 -Wall -W -Wshadow -Wwrite-strings -pthread 
LSHFLAG = -shared -Wl,--enable-auto-import,--export-all-symbols
DEPEND_PROG = $(CC) -MM -O2 -Wall -W -Wshadow -Wwrite-strings -pthread
FILTER = 
PIC_OPT = 
SOEXT = dll
DEVWINDLL = -L$(DLL_LOC) -lwrspice

else ifeq ($(OSNAME:Darwin%=Darwin),Darwin)

# Apple OS X
CXX = g++
CC = gcc
LINKCC = $(CXX)
CFLAGS = -O3 -Wall -W -Wshadow -Wwrite-strings -pthread 
LSHFLAG = -dynamiclib -undefined dynamic_lookup
DEPEND_PROG = $(CC) -MM
FILTER = 
PIC_OPT = 
SOEXT = dylib

else ifeq ($(OSNAME:Linux%=Linux),Linux)
ifeq ($(ARCH),x86_64)

# Generic Linux, covers all 64-bit builds.
CXX = g++
CC = gcc
LINKCC = $(CXX)
CFLAGS = -O3 -Wall -W -Wshadow -Wwrite-strings -pthread 
LSHFLAG = -shared
DEPEND_PROG = $(CC) -MM
FILTER = 
PIC_OPT = -fPIC
SOEXT = so

else

# Generic Linux, covers all 32-bit builds.
CXX = g++ -m32 -march=$(ARCH)
CC = gcc -m32 -march=$(ARCH)
LINKCC = $(CXX)
CFLAGS = -O3 -Wall -W -Wshadow -Wwrite-strings -pthread
LSHFLAG = -shared
DEPEND_PROG = $(CC) -MM -O3 -Wall -W -Wshadow -Wwrite-strings -pthread
FILTER =
PIC_OPT = -fPIC
SOEXT = so

endif
endif

TARGET = $(MODULE).$(SOEXT)
HFILES = $(MODULE)defs.h
CFILES =
CCFILES = \
  $(MODULE).cc \
  $(MODULE)acld.cc \
  $(MODULE)aski.cc \
  $(MODULE)askm.cc \
  $(MODULE)load.cc \
  $(MODULE)nois.cc \
  $(MODULE)pzld.cc \
  $(MODULE)set.cc \
  $(MODULE)seti.cc \
  $(MODULE)setm.cc \
  $(MODULE)topo.cc \
  $(MODULE)trun.cc

COBJS = $(CCFILES:.cc=.o) $(CFILES:.c=.o)
INCLUDE = -I. -I$(ADMST:admst=include)
DEFS = -D__WRMODULE__=$(MODULE) -D__WRVERSION__=$(DEVLIB_VERSION)

module:
	@echo
	@echo \
 "====> Building $(TARGET) for WRspice-$(DEVLIB_VERSION) $(OSNAME) $(ARCH)"
	@echo
	$(MAKE) $(TARGET)

$(TARGET): device.xml $(HFILES) $(COBJS)
	$(LINKCC) -o $(TARGET) $(LSHFLAG) $(COBJS) $(DEVWINDLL)

$(MODULE).o: $(MODULE).cc
	$(CXX) $(CFLAGS) $(PIC_OPT) $(INCLUDE) $(DEFS) -c $*.cc

.cc.o:
	$(CXX) $(CFLAGS) $(PIC_OPT) $(INCLUDE) -c $*.cc

.c.o:
	$(CC) $(CFLAGS) $(PIC_OPT) $(INCLUDE) -c $*.c

device.xml: Makefile
	@vers=$(DEVLIB_VERSION); IFS=.; set -- $$vers; \
	sed -e s/@MODULE@/$(MODULE)/ -e s/@MODELNAME@/$(MODELNAME)/ \
  -e s/@LEVEL@/$(LEVEL)/ -e s/@KEY@/$(KEY)/ -e s/@VERSION@/$(DEVLIB_VERSION)/ \
  -e s/@MAJOR@/$$1.$$2/ -e s/@MINOR@/$$3/ -e s/@ADD_M@/$(ADD_M)/ \
  -e s/@PREDICTOR@/$(PREDICTOR)/ -e s/@LIMITING@/$(LIMITING)/ \
  -e s/@FLAGS@/$(FLAGS)/ $(ADMST)/device.xml > device.xml
	@s=`grep SyntaxLevel device.xml`; set $$s; \
	if [ x$$2 != x$(SyntaxLevel) ]; then \
	    echo ;\
	    echo "  ERROR: Syntax Level mismatch: Makefile $(SyntaxLevel) \
scripts $$2, update Makefile.";\
	    echo ;\
            exit 1; \
	fi

depend:
	@echo depending in $(MODULE)
	@if [ x$(DEPEND_DONE) = x ]; then \
	    echo DEPEND_DONE = 1 >> Makefile; \
	    if [ "z$(CCFILES)" != "z" ]; then \
	        for a in $(CCFILES); do \
	            $(DEPEND_PROG) $(INCLUDE) $$a $(FILTER)>> Makefile; \
	        done \
	    fi; \
	    if [ "z$(CFILES)" != "z" ]; then \
	        for a in $(CFILES); do \
	            $(DEPEND_PROG) $(INCLUDE) $$a $(FILTER)>> Makefile; \
	        done \
	    fi; \
	fi

clean:
	-@rm -f $(COBJS) *.$(SOEXT)

realclean: clean
	-@rm -f device.xml $(HFILES) $(CCFILES) constant* discipline* \
  .adms.implicit.xml .interface.xml .*.adms

# This file is exported, don't blow away the user's Makefile!
distclean: realclean

# Predefine `WRspice in Veiilog input.
BASE = -DWRspice -x -e $(ADMST)/adms.implicit.xml -e device.xml -e \
 $(ADMST)/wrspiceVersion.xml

$(MODULE).cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULE.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULE.cc.xml

$(MODULE)acld.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEacld.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEacld.cc.xml

$(MODULE)aski.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEaski.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEaski.cc.xml

$(MODULE)askm.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEaskm.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEaskm.cc.xml

$(MODULE)load.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEload.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEload.cc.xml

$(MODULE)nois.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEnois.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEnois.cc.xml

$(MODULE)pzld.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEpzld.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEpzld.cc.xml

$(MODULE)set.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEset.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEset.cc.xml

$(MODULE)seti.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEseti.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEseti.cc.xml

$(MODULE)setm.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEsetm.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEsetm.cc.xml

# No longer used.
#$(MODULE)temp.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
#  $(ADMST)/wrspiceMODULEtemp.cc.xml $(ADMST)/adms.implicit.xml
#	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEtemp.cc.xml

$(MODULE)topo.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEtopo.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEtopo.cc.xml

$(MODULE)trun.cc: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEtrun.cc.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEtrun.cc.xml
	
$(MODULE)defs.h: $(SOURCE) device.xml $(ADMST)/wrspiceVersion.xml \
  $(ADMST)/wrspiceMODULEdefs.h.xml $(ADMST)/adms.implicit.xml
	$(ADMSXML) $(SOURCE) $(BASE) -e $(ADMST)/wrspiceMODULEdefs.h.xml

#######################################################################

