final gds & drc results
diff --git a/scripts/check_density.py b/scripts/check_density.py
new file mode 100755
index 0000000..7dceb44
--- /dev/null
+++ b/scripts/check_density.py
@@ -0,0 +1,601 @@
+#!/usr/bin/env python3
+# SPDX-FileCopyrightText: 2020 Efabless Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# SPDX-License-Identifier: Apache-2.0
+
+#
+# check_density.py ---
+#
+#    Run density checks on the final (filled) GDS.
+#
+
+import sys
+import os
+import re
+import select
+import subprocess
+
+def usage():
+    print("Usage:")
+    print("check_density.py [<path_to_project>] [-keep]")
+    print("")
+    print("where:")
+    print("   <path_to_project> is the path to the project top level directory.")
+    print("")
+    print("  If <path_to_project> is not given, then it is assumed to be the cwd.")
+    print("  If '-keep' is specified, then keep the check script.")
+    return 0
+
+
+if __name__ == '__main__':
+
+    optionlist = []
+    arguments = []
+
+    debugmode = False
+    keepmode = False
+
+    for option in sys.argv[1:]:
+        if option.find('-', 0) == 0:
+            optionlist.append(option)
+        else:
+            arguments.append(option)
+
+    if len(arguments) > 1:
+        print("Wrong number of arguments given to check_density.py.")
+        usage()
+        sys.exit(0)
+
+    if len(arguments) == 1:
+        user_project_path = arguments[0]
+    else:
+        user_project_path = os.getcwd()
+
+    # Check for valid user path
+
+    if not os.path.isdir(user_project_path):
+        print('Error:  Project path "' + user_project_path + '" does not exist or is not readable.')
+        sys.exit(1)
+
+    # Check for valid user ID
+    user_id_value = None
+    if os.path.isfile(user_project_path + '/info.yaml'):
+        with open(user_project_path + '/info.yaml', 'r') as ifile:
+            infolines = ifile.read().splitlines()
+            for line in infolines:
+                kvpair = line.split(':')
+                if len(kvpair) == 2:
+                    key = kvpair[0].strip()
+                    value = kvpair[1].strip()
+                    if key == 'project_id':
+                        user_id_value = value.strip('"\'')
+                        break
+
+    if user_id_value:
+        project = 'caravel'
+        project_with_id = 'caravel_' + user_id_value
+    else:
+        print('Error:  No project_id found in info.yaml file.')
+        sys.exit(1)
+
+    if '-debug' in optionlist:
+        debugmode = True
+    if '-keep' in optionlist:
+        keepmode = True
+
+    magpath = user_project_path + '/mag'
+    rcfile = magpath + '/.magicrc'
+
+    with open(magpath + '/check_density.tcl', 'w') as ofile:
+        print('#!/bin/env wish', file=ofile)
+        print('crashbackups stop', file=ofile)
+        print('drc off', file=ofile)
+        print('snap internal', file=ofile)
+
+        print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
+        print('puts stdout "Started reading GDS: $starttime"', file=ofile)
+        print('', file=ofile)
+        print('flush stdout', file=ofile)
+        print('update idletasks', file=ofile)
+
+        # Read final project from .gds
+        print('gds readonly true', file=ofile)
+        print('gds rescale false', file=ofile)
+        print('gds read ../gds/' + project_with_id + '.gds', file=ofile)
+        print('', file=ofile)
+
+        print('set midtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
+        print('puts stdout "Starting density checks: $midtime"', file=ofile)
+        print('', file=ofile)
+        print('flush stdout', file=ofile)
+        print('update idletasks', file=ofile)
+
+        # Get step box dimensions (700um for size and 70um for step)
+        print('box values 0 0 0 0', file=ofile)
+        # print('box size 700um 700um', file=ofile)
+        # print('set stepbox [box values]', file=ofile)
+        # print('set stepwidth [lindex $stepbox 2]', file=ofile)
+        # print('set stepheight [lindex $stepbox 3]', file=ofile)
+
+        print('box size 70um 70um', file=ofile)
+        print('set stepbox [box values]', file=ofile)
+        print('set stepsizex [lindex $stepbox 2]', file=ofile)
+        print('set stepsizey [lindex $stepbox 3]', file=ofile)
+
+        print('select top cell', file=ofile)
+        print('expand', file=ofile)
+        print('set fullbox [box values]', file=ofile)
+        print('set xmax [lindex $fullbox 2]', file=ofile)
+        print('set xmin [lindex $fullbox 0]', file=ofile)
+        print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
+        print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepsizex))}]', file=ofile)
+        print('set ymax [lindex $fullbox 3]', file=ofile)
+        print('set ymin [lindex $fullbox 1]', file=ofile)
+        print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
+        print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepsizey))}]', file=ofile)
+        print('box size $stepsizex $stepsizey', file=ofile)
+        print('set xbase [lindex $fullbox 0]', file=ofile)
+        print('set ybase [lindex $fullbox 1]', file=ofile)
+        print('', file=ofile)
+
+        print('puts stdout "XTILES: $xtiles"', file=ofile)
+        print('puts stdout "YTILES: $ytiles"', file=ofile)
+        print('', file=ofile)
+
+        # Need to know what fraction of a full tile is the last row and column
+        print('set xfrac [expr {($xtiles * $stepsizex - $fullwidth + 0.0) / $stepsizex}]', file=ofile)
+        print('set yfrac [expr {($ytiles * $stepsizey - $fullheight + 0.0) / $stepsizey}]', file=ofile)
+        print('puts stdout "XFRAC: $xfrac"', file=ofile)
+        print('puts stdout "YFRAC: $yfrac"', file=ofile)
+
+        print('cif ostyle density', file=ofile)
+
+        # Process density at steps.  For efficiency, this is done in 70x70 um
+        # areas, dumped to a file, and then aggregated into the 700x700 areas.
+
+        print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
+        print('    for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
+        print('        set xlo [expr $xbase + $x * $stepsizex]', file=ofile)
+        print('        set ylo [expr $ybase + $y * $stepsizey]', file=ofile)
+        print('        set xhi [expr $xlo + $stepsizex]', file=ofile)
+        print('        set yhi [expr $ylo + $stepsizey]', file=ofile)
+        print('        box values $xlo $ylo $xhi $yhi', file=ofile)
+
+        # Flatten this area
+        print('        flatten -dobbox -nolabels tile', file=ofile)
+        print('        load tile', file=ofile)
+        print('        select top cell', file=ofile)
+
+        # Run density check for each layer
+        print('        puts stdout "Density results for tile x=$x y=$y"', file=ofile)
+
+        print('        set fdens  [cif list cover fom_all]', file=ofile)
+        print('        set pdens  [cif list cover poly_all]', file=ofile)
+        print('        set ldens  [cif list cover li_all]', file=ofile)
+        print('        set m1dens [cif list cover m1_all]', file=ofile)
+        print('        set m2dens [cif list cover m2_all]', file=ofile)
+        print('        set m3dens [cif list cover m3_all]', file=ofile)
+        print('        set m4dens [cif list cover m4_all]', file=ofile)
+        print('        set m5dens [cif list cover m5_all]', file=ofile)
+        print('        puts stdout "FOM: $fdens"', file=ofile)
+        print('        puts stdout "POLY: $pdens"', file=ofile)
+        print('        puts stdout "LI1: $ldens"', file=ofile)
+        print('        puts stdout "MET1: $m1dens"', file=ofile)
+        print('        puts stdout "MET2: $m2dens"', file=ofile)
+        print('        puts stdout "MET3: $m3dens"', file=ofile)
+        print('        puts stdout "MET4: $m4dens"', file=ofile)
+        print('        puts stdout "MET5: $m5dens"', file=ofile)
+        print('        flush stdout', file=ofile)
+        print('        update idletasks', file=ofile)
+
+        print('        load ' + project_with_id, file=ofile)
+        print('        cellname delete tile', file=ofile)
+
+        print('    }', file=ofile)
+        print('}', file=ofile)
+
+        print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
+        print('puts stdout "Ended: $endtime"', file=ofile)
+        print('', file=ofile)
+
+
+    myenv = os.environ.copy()
+    # Real views are necessary for the DRC checks
+    myenv['MAGTYPE'] = 'mag'
+
+    print('Running density checks on file ' + project_with_id + '.gds', flush=True)
+
+    mproc = subprocess.Popen(['magic', '-dnull', '-noconsole',
+		'-rcfile', rcfile, magpath + '/check_density.tcl'],
+		stdin = subprocess.DEVNULL,
+		stdout = subprocess.PIPE,
+		stderr = subprocess.PIPE,
+		cwd = magpath,
+		env = myenv,
+		universal_newlines = True)
+
+    # Use signal to poll the process and generate any output as it arrives
+
+    dlines = []
+
+    while mproc:
+        status = mproc.poll()
+        if status != None:
+            try:
+                output = mproc.communicate(timeout=1)
+            except ValueError:
+                print('Magic forced stop, status ' + str(status))
+                sys.exit(1)
+            else:
+                outlines = output[0]
+                errlines = output[1]
+                for line in outlines.splitlines():
+                    dlines.append(line)
+                    print(line)
+                for line in errlines.splitlines():
+                    print(line)
+                print('Magic exited with status ' + str(status))
+                if int(status) != 0:
+                    sys.exit(int(status))
+                else:
+                    break
+        else:
+            n = 0
+            while True:
+                n += 1
+                if n > 100:
+                    n = 0
+                    status = mproc.poll()
+                    if status != None:
+                        break
+                sresult = select.select([mproc.stdout, mproc.stderr], [], [], 0)[0]
+                if mproc.stdout in sresult:
+                    outstring = mproc.stdout.readline().strip()
+                    dlines.append(outstring)
+                    print(outstring)
+                elif mproc.stderr in sresult:
+                    outstring = mproc.stderr.readline().strip()
+                    print(outstring)
+                else:
+                    break
+
+    fomfill  = []
+    polyfill = []
+    lifill   = []
+    met1fill = []
+    met2fill = []
+    met3fill = []
+    met4fill = []
+    met5fill = []
+    xtiles = 0
+    ytiles = 0
+    xfrac = 0.0
+    yfrac = 0.0
+
+    for line in dlines:
+        dpair = line.split(':')
+        if len(dpair) == 2:
+            layer = dpair[0]
+            try:
+                density = float(dpair[1].strip())
+            except:
+                continue
+            if layer == 'FOM':
+                fomfill.append(density)
+            elif layer == 'POLY':
+                polyfill.append(density)
+            elif layer == 'LI1':
+                lifill.append(density)
+            elif layer == 'MET1':
+                met1fill.append(density)
+            elif layer == 'MET2':
+                met2fill.append(density)
+            elif layer == 'MET3':
+                met3fill.append(density)
+            elif layer == 'MET4':
+                met4fill.append(density)
+            elif layer == 'MET5':
+                met5fill.append(density)
+            elif layer == 'XTILES':
+                xtiles = int(dpair[1].strip())
+            elif layer == 'YTILES':
+                ytiles = int(dpair[1].strip())
+            elif layer == 'XFRAC':
+                xfrac = float(dpair[1].strip())
+            elif layer == 'YFRAC':
+                yfrac = float(dpair[1].strip())
+
+    if ytiles == 0 or xtiles == 0:
+        print('Failed to read XTILES or YTILES from output.')
+        sys.exit(1)
+
+    total_tiles = (ytiles - 9) * (xtiles - 9)
+
+    print('')
+    print('Density results (total tiles = ' + str(total_tiles) + '):')
+
+    # Full areas are 10 x 10 tiles = 100.  But the right and top sides are
+    # not full tiles, so the full area must be prorated.
+
+    sideadjust = 90.0 + (10.0 * xfrac)
+    topadjust = 90.0 + (10.0 * yfrac)
+    corneradjust = 81.0 + (9.0 * xfrac) + (9.0 * yfrac) + (xfrac * yfrac)
+
+    print('')
+    print('FOM Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            fomaccum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                fomaccum += sum(fomfill[base : base + 10])
+                    
+            fomaccum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(fomaccum))
+            if fomaccum < 0.33:
+                print('***Error:  FOM Density < 33%')
+            elif fomaccum > 0.57:
+                print('***Error:  FOM Density > 57%')
+
+    print('')
+    print('POLY Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            polyaccum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                polyaccum += sum(polyfill[base : base + 10])
+                    
+            polyaccum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(polyaccum))
+
+    print('')
+    print('LI Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            liaccum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                liaccum += sum(lifill[base : base + 10])
+                    
+            liaccum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(liaccum))
+            if liaccum < 0.35:
+                print('***Error:  LI Density < 35%')
+            elif liaccum > 0.70:
+                print('***Error:  LI Density > 70%')
+
+    print('')
+    print('MET1 Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            met1accum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                met1accum += sum(met1fill[base : base + 10])
+                    
+            met1accum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(met1accum))
+            if met1accum < 0.35:
+                print('***Error:  MET1 Density < 35%')
+            elif met1accum > 0.70:
+                print('***Error:  MET1 Density > 70%')
+
+    print('')
+    print('MET2 Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            met2accum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                met2accum += sum(met2fill[base : base + 10])
+                    
+            met2accum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(met2accum))
+            if met2accum < 0.35:
+                print('***Error:  MET2 Density < 35%')
+            elif met2accum > 0.70:
+                print('***Error:  MET2 Density > 70%')
+
+    print('')
+    print('MET3 Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            met3accum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                met3accum += sum(met3fill[base : base + 10])
+                    
+            met3accum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(met3accum))
+            if met3accum < 0.35:
+                print('***Error:  MET3 Density < 35%')
+            elif met3accum > 0.70:
+                print('***Error:  MET3 Density > 70%')
+
+    print('')
+    print('MET4 Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            met4accum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                met4accum += sum(met4fill[base : base + 10])
+                    
+            met4accum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(met4accum))
+            if met4accum < 0.35:
+                print('***Error:  MET4 Density < 35%')
+            elif met4accum > 0.70:
+                print('***Error:  MET4 Density > 70%')
+
+    print('')
+    print('MET5 Density:')
+    for y in range(0, ytiles - 9):
+        if y == ytiles - 10:
+            atotal = topadjust
+        else:
+            atotal = 100.0
+        for x in range(0, xtiles - 9):
+            if x == xtiles - 10:
+                if y == ytiles - 10:
+                    atotal = corneradjust
+                else:
+                    atotal = sideadjust
+            met5accum = 0
+            for w in range(y, y + 10):
+                base = xtiles * w + x
+                met5accum += sum(met5fill[base : base + 10])
+                    
+            met5accum /= atotal
+            print('Tile (' + str(x) + ', ' + str(y) + '):   ' + str(met5accum))
+            if met5accum < 0.45:
+                print('***Error:  MET5 Density < 45%')
+            elif met5accum > 0.86:
+                print('***Error:  MET5 Density > 86%')
+
+    print('')
+    print('Whole-chip density results:')
+
+    atotal = ((xtiles - 1.0) * (ytiles - 1.0)) + ((ytiles - 1.0) * xfrac) + ((xtiles - 1.0) * yfrac) + (xfrac * yfrac)
+
+    fomaccum = sum(fomfill) / atotal
+    print('')
+    print('FOM Density: ' + str(fomaccum))
+    if fomaccum < 0.33:
+        print('***Error:  FOM Density < 33%')
+    elif fomaccum > 0.57:
+        print('***Error:  FOM Density > 57%')
+
+    polyaccum = sum(polyfill) / atotal
+    print('')
+    print('POLY Density: ' + str(polyaccum))
+
+    liaccum = sum(lifill) / atotal
+    print('')
+    print('LI Density: ' + str(liaccum))
+    if liaccum < 0.35:
+        print('***Error:  LI Density < 35%')
+    elif liaccum > 0.70:
+        print('***Error:  LI Density > 70%')
+
+    met1accum = sum(met1fill) / atotal
+    print('')
+    print('MET1 Density: ' + str(met1accum))
+    if met1accum < 0.35:
+        print('***Error:  MET1 Density < 35%')
+    elif met1accum > 0.70:
+        print('***Error:  MET1 Density > 70%')
+
+    met2accum = sum(met2fill) / atotal
+    print('')
+    print('MET2 Density: ' + str(met2accum))
+    if met2accum < 0.35:
+        print('***Error:  MET2 Density < 35%')
+    elif met2accum > 0.70:
+        print('***Error:  MET2 Density > 70%')
+
+    met3accum = sum(met3fill) / atotal
+    print('')
+    print('MET3 Density: ' + str(met3accum))
+    if met3accum < 0.35:
+        print('***Error:  MET3 Density < 35%')
+    elif met3accum > 0.70:
+        print('***Error:  MET3 Density > 70%')
+
+    met4accum = sum(met4fill) / atotal
+    print('')
+    print('MET4 Density: ' + str(met4accum))
+    if met4accum < 0.35:
+        print('***Error:  MET4 Density < 35%')
+    elif met4accum > 0.70:
+        print('***Error:  MET4 Density > 70%')
+
+    met5accum = sum(met5fill) / atotal
+    print('')
+    print('MET5 Density: ' + str(met5accum))
+    if met5accum < 0.45:
+        print('***Error:  MET5 Density < 45%')
+    elif met5accum > 0.86:
+        print('***Error:  MET5 Density > 86%')
+
+    if not keepmode:
+        os.remove(magpath + '/check_density.tcl')
+
+    print('')
+    print('Done!')
+    sys.exit(0)
diff --git a/scripts/compositor.py b/scripts/compositor.py
index ec48636..ffda740 100755
--- a/scripts/compositor.py
+++ b/scripts/compositor.py
@@ -1,4 +1,4 @@
-#!/bin/env python3
+#!/usr/bin/env python3
 # SPDX-FileCopyrightText: 2020 Efabless Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,15 +27,18 @@
 import subprocess
 
 def usage():
-    print("compositor.py [layout_name] [-keep]")
+    print("Usage:")
+    print("compositor.py [<path_to_project>] [-keep]")
+    print("")
+    print("where:")
+    print("   <path_to_project> is the path to the project top level directory.")
+    print("")
+    print("  If <path_to_project> is not given, then it is assumed to be the cwd.")
+    print("  If '-keep' is specified, then keep the generation script.")
     return 0
 
 if __name__ == '__main__':
 
-    if len(sys.argv) == 1:
-        usage()
-        sys.exit(0)
-
     optionlist = []
     arguments = []
 
@@ -54,80 +57,139 @@
         sys.exit(0)
 
     if len(arguments) == 1:
-        project = arguments[0]
+        user_project_path = arguments[0]
     else:
+        user_project_path = os.getcwd()
+
+    # Check for valid user path
+
+    if not os.path.isdir(user_project_path):
+        print('Error:  Project path "' + user_project_path + '" does not exist or is not readable.')
+        sys.exit(1)
+
+    # Check for valid user ID
+    user_id_value = None
+    if os.path.isfile(user_project_path + '/info.yaml'):
+        with open(user_project_path + '/info.yaml', 'r') as ifile:
+            infolines = ifile.read().splitlines()
+            for line in infolines:
+                kvpair = line.split(':')
+                if len(kvpair) == 2:
+                    key = kvpair[0].strip()
+                    value = kvpair[1].strip()
+                    if key == 'project_id':
+                        user_id_value = value.strip('"\'')
+                        break
+
+    if user_id_value:
         project = 'caravel'
+        project_with_id = 'caravel_' + user_id_value
+    else:
+        print('Error:  No project_id found in info.yaml file.')
+        sys.exit(1)
 
     if '-debug' in optionlist:
         debugmode = True
     if '-keep' in optionlist:
         keepmode = True
 
-    magdir = '../mag'
-    rcfile = magdir + '/.magicrc'
+    magpath = user_project_path + '/mag'
+    rcfile = magpath + '/.magicrc'
 
-    with open(magdir + '/compose_final.tcl', 'w') as ofile:
+    # The compositor script will create <project_with_id>.mag, but is uses
+    # "load", so the file must not already exist.
+
+    if os.path.isfile(magpath + '/' + project_with_id + '.mag'):
+        print('Error:  File ' + project_with_id + '.mag exists already!  Exiting. . .')
+        sys.exit(1)
+
+    with open(magpath + '/compose_final.tcl', 'w') as ofile:
         print('#!/bin/env wish', file=ofile)
         print('drc off', file=ofile)
 
+        # Read project from .mag but set GDS properties so that it points
+        # to the GDS file created by "make ship".
         print('load ' + project + ' -dereference', file=ofile)
+        print('property GDS_FILE ../gds/' + project + '.gds', file=ofile)
+        print('property GDS_START 0', file=ofile)
         print('select top cell', file=ofile)
+        print('set bbox [box values]', file=ofile)
 
         # Ceate a cell to represent the generated fill.  There are
         # no magic layers corresponding to the fill shape data, and
         # it's gigabytes anyway, so we don't want to deal with any
         # actual data.  So it's just a placeholder.
 
-        print('set bbox [box values]', file=ofile)
-        print('load ' + project + '_fill_pattern', file=ofile)
+        print('load ' + project_with_id + '_fill_pattern', file=ofile)
         print('snap internal', file=ofile)
         print('box values {*}$bbox', file=ofile)
         print('paint comment', file=ofile)
-        print('property GDS_FILE ../gds/' + project + '_fill_pattern.gds', file=ofile)
+        print('property GDS_FILE ../gds/' + project_with_id + '_fill_pattern.gds', file=ofile)
         print('property GDS_START 0', file=ofile)
         print('property FIXED_BBOX "$bbox"', file=ofile)
 
-        # Now go back to the project top level and place the fill cell.
-        print('load ' + project, file=ofile)
-        print('select top cell', file=ofile)	
-        print('getcell ' + project + '_fill_pattern child 0 0', file=ofile)
+        # Create a new project top level and place the fill cell.
+        print('load ' + project_with_id + ' -quiet', file=ofile)
+        print('box values 0 0 0 0', file=ofile)	
+        print('box position 6um 6um', file=ofile)	
+        print('getcell ' + project + ' child 0 0', file=ofile)
+        print('getcell ' + project_with_id + '_fill_pattern child 0 0', file=ofile)
 
         # Move existing origin to (6um, 6um) for seal ring placement
-        print('move origin -6um -6um', file=ofile)
+        # print('move origin -6um -6um', file=ofile)
 
         # Read in abstract view of seal ring
         print('box position 0 0', file=ofile)
         print('getcell advSeal_6um_gen', file=ofile)
 
+        # Write out completed project as "caravel_" + the user ID
+        print('save ' + project_with_id, file=ofile)
+
         # Generate final GDS
         print('puts stdout "Writing final GDS. . . "', file=ofile)
         print('flush stdout', file=ofile)
-        print('gds write ../gds/' + project + '_final.gds', file=ofile)
+        print('cif *hier write disable', file=ofile)
+        print('gds write ../gds/' + project_with_id + '.gds', file=ofile)
         print('quit -noprompt', file=ofile)
 
     myenv = os.environ.copy()
     # Abstract views are appropriate for final composition
     myenv['MAGTYPE'] = 'maglef'
 
+    print('Building final GDS file ' + project_with_id + '.gds', flush=True)
+
     mproc = subprocess.run(['magic', '-dnull', '-noconsole',
-		'-rcfile', rcfile, magdir + '/compose_final.tcl'],
+		'-rcfile', rcfile, magpath + '/compose_final.tcl'],
 		stdin = subprocess.DEVNULL,
 		stdout = subprocess.PIPE,
 		stderr = subprocess.PIPE,
-		cwd = magdir,
+		cwd = magpath,
 		env = myenv,
 		universal_newlines = True)
     if mproc.stdout:
         for line in mproc.stdout.splitlines():
             print(line)
     if mproc.stderr:
-        print('Error message output from magic:')
+        # NOTE:  Until there is a "load -quiet" option in magic, loading
+        # a new cell generates an error.  This code ignores the error.
+        newlines = []
         for line in mproc.stderr.splitlines():
-            print(line)
+            if line.endswith("_fill_pattern.mag couldn't be read"):
+                continue
+            if line.startswith("No such file or directory"):
+                continue
+            else:
+                newlines.append(line)
+
+        if len(newlines) > 0:
+            print('Error message output from magic:')
+            for line in newlines:
+                print(line)
         if mproc.returncode != 0:
             print('ERROR:  Magic exited with status ' + str(mproc.returncode))
 
     if not keepmode:
-        os.remove(magdir + '/compose_final.tcl')
+        os.remove(magpath + '/compose_final.tcl')
 
+    print('Done!')
     exit(0)
diff --git a/scripts/count_lvs.py b/scripts/count_lvs.py
new file mode 100644
index 0000000..21b7771
--- /dev/null
+++ b/scripts/count_lvs.py
@@ -0,0 +1,132 @@
+#!/usr/bin/python3
+# SPDX-FileCopyrightText: 2020 Efabless Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# SPDX-License-Identifier: Apache-2.0
+
+#
+#---------------------------------------------------------
+# LVS failure check
+#
+# This is a Python script that parses the comp.json
+# output from netgen and reports on the number of
+# errors in the top-level netlist.
+#
+#---------------------------------------------------------
+# Written by Tim Edwards
+# efabless, inc.
+# Pulled from qflow GUI as standalone script Aug 20, 2018
+#---------------------------------------------------------
+
+import os
+import re
+import sys
+import json
+import argparse
+
+def count_LVS_failures(filename):
+    with open(filename, 'r') as cfile:
+        lvsdata = json.load(cfile)
+
+    # Count errors in the JSON file
+    failures = 0
+    devfail = 0
+    netfail = 0
+    pinfail = 0
+    propfail = 0
+    netdiff = 0
+    devdiff = 0
+    ncells = len(lvsdata)
+    for c in range(0, ncells):
+        cellrec = lvsdata[c]
+
+        if c == ncells - 1:
+            topcell = True
+        else:
+            topcell = False
+
+        # Most errors must only be counted for the top cell, because individual
+        # failing cells are flattened and the matching attempted again on the
+        # flattened netlist.
+
+        if topcell:
+            if 'devices' in cellrec:
+                devices = cellrec['devices']
+                devlist = [val for pair in zip(devices[0], devices[1]) for val in pair]
+                devpair = list(devlist[p:p + 2] for p in range(0, len(devlist), 2))
+                for dev in devpair:
+                    c1dev = dev[0]
+                    c2dev = dev[1]
+                    diffdevs = abs(c1dev[1] - c2dev[1])
+                    failures += diffdevs
+                    devdiff += diffdevs
+
+            if 'nets' in cellrec:
+                nets = cellrec['nets']
+                diffnets = abs(nets[0] - nets[1])
+                failures += diffnets
+                netdiff += diffnets
+
+            if 'badnets' in cellrec:
+                badnets = cellrec['badnets']
+                failures += len(badnets)
+                netfail += len(badnets)
+
+            if 'badelements' in cellrec:
+                badelements = cellrec['badelements']
+                failures += len(badelements)
+                devfail += len(badelements)
+
+            if 'pins' in cellrec:
+                pins = cellrec['pins']
+                pinlist = [val for pair in zip(pins[0], pins[1]) for val in pair]
+                pinpair = list(pinlist[p:p + 2] for p in range(0, len(pinlist), 2))
+                for pin in pinpair:
+                    # Avoid flagging global vs. local names, e.g., "gnd" vs. "gnd!,"
+                    # and ignore case when comparing pins.
+                    pin0 = re.sub('!$', '', pin[0].lower())
+                    pin1 = re.sub('!$', '', pin[1].lower())
+                    if pin0 != pin1:
+                        failures += 1
+                        pinfail += 1
+
+        # Property errors must be counted for every cell
+        if 'properties' in cellrec:
+            properties = cellrec['properties']
+            failures += len(properties)
+            propfail += len(properties)
+
+    return [failures, netfail, devfail, pinfail, propfail, netdiff, devdiff]
+
+if __name__ == '__main__':
+
+    parser = argparse.ArgumentParser(description='Parses netgen lvs')
+    parser.add_argument('--file', '-f', required=True)
+    args = parser.parse_args()
+    failures = count_LVS_failures(args.file)
+    total = failures[0]
+    if total > 0:
+        failed = True
+        print('LVS reports:')
+        print('    net count difference = ' + str(failures[5]))
+        print('    device count difference = ' + str(failures[6]))
+        print('    unmatched nets = ' + str(failures[1]))
+        print('    unmatched devices = ' + str(failures[2]))
+        print('    unmatched pins = ' + str(failures[3]))
+        print('    property failures = ' + str(failures[4]))
+    else:
+        print('LVS reports no net, device, pin, or property mismatches.')
+
+    print('')
+    print('Total errors = ' + str(total))
+ 
diff --git a/scripts/create-caravel-diagram.py b/scripts/create-caravel-diagram.py
new file mode 100644
index 0000000..18af64d
--- /dev/null
+++ b/scripts/create-caravel-diagram.py
@@ -0,0 +1,112 @@
+import sys
+import os
+import subprocess
+from pathlib import Path
+import argparse
+from tempfile import mkstemp
+import re
+
+
+def remove_inouts(jsonpath, replacewith='input'):
+    """Replaces inouts with either input or output statements.
+
+    Netlistsvg does not parse inout ports as for now, so they need to be
+    replaced with either input or output to produce a diagram.
+
+    Parameters
+    ----------
+    jsonpath : str
+        Path to JSON file to fix
+    replacewith : str
+        The string to replace 'inout', can be 'input' or 'output'
+    """
+    assert replacewith in ['input', 'output']
+    with open(jsonpath, 'r') as withinouts:
+        lines = withinouts.readlines()
+    with open(jsonpath, 'w') as withoutinouts:
+        for line in lines:
+            withoutinouts.write(re.sub('inout', replacewith, line))
+
+
+def main(argv):
+    parser = argparse.ArgumentParser(argv[0])
+    parser.add_argument(
+        'verilog_rtl_dir',
+        help="Path to the project's verilog/rtl directory",
+        type=Path)
+    parser.add_argument(
+        'output',
+        help="Path to the output SVG file",
+        type=Path)
+    parser.add_argument(
+        '--num-iopads',
+        help='Number of iopads to render',
+        type=int,
+        default=38)
+    parser.add_argument(
+        '--yosys-executable',
+        help='Path to yosys executable',
+        type=Path,
+        default='yosys')
+    parser.add_argument(
+        '--netlistsvg-executable',
+        help='Path to netlistsvg executable',
+        type=Path,
+        default='netlistsvg')
+    parser.add_argument(
+        '--inouts-as',
+        help='To what kind of IO should inout ports be replaced',
+        choices=['input', 'output'],
+        default='input'
+    )
+
+    args = parser.parse_args(argv[1:])
+
+    fd, jsonpath = mkstemp(suffix='-yosys.json')
+    os.close(fd)
+
+    yosyscommand = [
+        f'{str(args.yosys_executable)}',
+        '-p',
+        'read_verilog pads.v defines.v; ' +
+        'read_verilog -lib -overwrite *.v; ' +
+        f'verilog_defines -DMPRJ_IO_PADS={args.num_iopads}; ' +
+        'read_verilog -overwrite caravel.v; ' +
+        'hierarchy -top caravel; ' +
+        'proc; ' +
+        'opt; ' +
+        f'write_json {jsonpath}; '
+    ]
+
+    result = subprocess.run(
+        yosyscommand,
+        cwd=args.verilog_rtl_dir,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT
+    )
+
+    exitcode = 0
+    if result.returncode != 0:
+        print(f'Failed to run: {" ".join(yosyscommand)}', file=sys.stderr)
+        print(result.stdout.decode())
+        exitcode = result.returncode
+    else:
+        # TODO once netlistsvg supports inout ports, this should be removed
+        remove_inouts(jsonpath, args.inouts_as)
+        command = f'{args.netlistsvg_executable} {jsonpath} -o {args.output}'
+        result = subprocess.run(
+            command.split(),
+            stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT
+        )
+        if result.returncode != 0:
+            print(f'Failed to run: {command}', file=sys.stderr)
+            print(result.stdout.decode())
+            exitcode = result.returncode
+
+    os.unlink(jsonpath)
+    sys.exit(exitcode)
+
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
diff --git a/scripts/generate_fill.py b/scripts/generate_fill.py
index 6b4cbcd..97e8141 100755
--- a/scripts/generate_fill.py
+++ b/scripts/generate_fill.py
@@ -1,4 +1,4 @@
-#!/bin/env python3
+#!/usr/bin/env python3
 # SPDX-FileCopyrightText: 2020 Efabless Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,7 +17,7 @@
 #
 # generate_fill.py ---
 #
-#    Run the fill generation on the caravel top level.
+#    Run the fill generation on a layout top level.
 #
 
 import sys
@@ -26,20 +26,25 @@
 import subprocess
 
 def usage():
-    print("generate_fill.py [layout_name] [-keep]")
+    print("Usage:")
+    print("generate_fill.py [<path_to_project>] [-keep] [-test]")
+    print("")
+    print("where:")
+    print("    <path_to_project> is the path to the project top level directory.")
+    print("")
+    print("  If <path_to_project> is not given, then it is assumed to be the cwd.")
+    print("  If '-keep' is specified, then keep the generation script.")
+    print("  If '-test' is specified, then create but do not run the generation script.")
     return 0
 
 if __name__ == '__main__':
 
-    if len(sys.argv) == 1:
-        usage()
-        sys.exit(0)
-
     optionlist = []
     arguments = []
 
     debugmode = False
     keepmode = False
+    testmode = False
 
     for option in sys.argv[1:]:
         if option.find('-', 0) == 0:
@@ -50,62 +55,212 @@
     if len(arguments) > 1:
         print("Wrong number of arguments given to generate_fill.py.")
         usage()
-        sys.exit(0)
+        sys.exit(1)
 
     if len(arguments) == 1:
-        project = arguments[0]
+        user_project_path = arguments[0]
     else:
-        project = 'caravel'
+        user_project_path = os.getcwd()
+
+    if not os.path.isdir(user_project_path):
+        print('Error:  Project path "' + user_project_path + '" does not exist or is not readable.')
+        sys.exit(1)
+
+    # Check for valid user ID
+    user_id_value = None
+    if os.path.isfile(user_project_path + '/info.yaml'):
+        with open(user_project_path + '/info.yaml', 'r') as ifile:
+            infolines = ifile.read().splitlines()
+            for line in infolines:
+                kvpair = line.split(':')
+                if len(kvpair) == 2:
+                    key = kvpair[0].strip()
+                    value = kvpair[1].strip()
+                    if key == 'project_id':
+                        user_id_value = value.strip('"\'')
+                        break
+
+    project = 'caravel'
+    if user_id_value:
+        project_with_id = project + '_' + user_id_value
+    else:
+        print('Error:  No project_id found in info.yaml file.')
+        sys.exit(1)
 
     if '-debug' in optionlist:
         debugmode = True
     if '-keep' in optionlist:
         keepmode = True
+    if '-test' in optionlist:
+        testmode = True
 
-    magdir = '../mag'
-    rcfile = magdir + '/.magicrc'
+    magpath = user_project_path + '/mag'
+    rcfile = magpath + '/.magicrc'
 
-    with open(magdir + '/generate_fill.tcl', 'w') as ofile:
+    if not os.path.isfile(rcfile):
+        rcfile = None
+
+    topdir = user_project_path
+    gdsdir = topdir + '/gds'
+    hasgdsdir = True if os.path.isdir(gdsdir) else False
+
+    with open(magpath + '/generate_fill.tcl', 'w') as ofile:
         print('#!/bin/env wish', file=ofile)
         print('drc off', file=ofile)
-        print('load ' + project + ' -dereference', file=ofile)
+        print('tech unlock *', file=ofile)
+        print('snap internal', file=ofile)
+        print('box values 0 0 0 0', file=ofile)
+        print('box size 700um 700um', file=ofile)
+        print('set stepbox [box values]', file=ofile)
+        print('set stepwidth [lindex $stepbox 2]', file=ofile)
+        print('set stepheight [lindex $stepbox 3]', file=ofile)
+        print('', file=ofile)
+        print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
+        print('puts stdout "Started: $starttime"', file=ofile)
+        print('', file=ofile)
+        # Read the user project from GDS, as there is not necessarily a magic database file
+        # to go along with this.
+        # print('gds read ../gds/user_project_wrapper', file=ofile)
+        # Now read the full caravel project
+        # print('load ' + project + ' -dereference', file=ofile)
+        print('gds readonly true', file=ofile)
+        print('gds rescale false', file=ofile)
+        print('gds read ../gds/caravel', file=ofile)
         print('select top cell', file=ofile)
         print('expand', file=ofile)
+        print('cif ostyle wafflefill(tiled)', file=ofile)
+        print('', file=ofile)
+        print('set fullbox [box values]', file=ofile)
+        print('set xmax [lindex $fullbox 2]', file=ofile)
+        print('set xmin [lindex $fullbox 0]', file=ofile)
+        print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
+        print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepwidth))}]', file=ofile)
+        print('set ymax [lindex $fullbox 3]', file=ofile)
+        print('set ymin [lindex $fullbox 1]', file=ofile)
+        print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
+        print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepheight))}]', file=ofile)
+        print('box size $stepwidth $stepheight', file=ofile)
+        print('set xbase [lindex $fullbox 0]', file=ofile)
+        print('set ybase [lindex $fullbox 1]', file=ofile)
+        print('', file=ofile)
+
+        # Break layout into tiles and process each separately
+        print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
+        print('    for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
+        print('        set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
+        print('        set ylo [expr $ybase + $y * $stepheight]', file=ofile)
+        print('        set xhi [expr $xlo + $stepwidth]', file=ofile)
+        print('        set yhi [expr $ylo + $stepheight]', file=ofile)
+        print('        if {$xhi > $fullwidth} {set xhi $fullwidth}', file=ofile)
+        print('        if {$yhi > $fullheight} {set yhi $fullheight}', file=ofile)
+        print('        box values $xlo $ylo $xhi $yhi', file=ofile)
+        # The flattened area must be larger than the fill tile by >1.5um
+        print('        box grow c 1.6um', file=ofile)
 
         # Flatten into a cell with a new name
-        print('puts stdout "Flattening layout. . . "', file=ofile)
-        print('flatten -nolabels ' + project + '_fill_pattern', file=ofile)
-        print('load ' + project + '_fill_pattern', file=ofile)
+        print('        puts stdout "Flattening layout of tile x=$x y=$y. . . "', file=ofile)
+        print('        flush stdout', file=ofile)
+        print('        update idletasks', file=ofile)
+        print('        flatten -dobox -nolabels ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
+        print('        load ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
 
-        # Remove any GDS_FILE reference
-        print('property GDS_FILE ""', file=ofile)
-        print('cif ostyle wafflefill', file=ofile)
-        print('puts stdout "Writing GDS. . . "', file=ofile)
-        print('gds write ../gds/' + project + '_fill_pattern.gds', file=ofile)
+        # Remove any GDS_FILE reference (there should not be any?)
+        print('        property GDS_FILE ""', file=ofile)
+        # Set boundary using comment layer, to the size of the step box
+	# This corresponds to the "topbox" rule in the wafflefill(tiled) style
+        print('        select top cell', file=ofile)
+        print('        erase comment', file=ofile)
+        print('        box values $xlo $ylo $xhi $yhi', file=ofile)
+        print('        paint comment', file=ofile)
+        print('        puts stdout "Writing GDS. . . "', file=ofile)
+        print('        flush stdout', file=ofile)
+        print('        update idletasks', file=ofile)
+        print('        gds write ' + project_with_id + '_fill_pattern_${x}_$y.gds', file=ofile)
+
+        # Reload project top
+        print('        load ' + project, file=ofile)
+
+        # Remove last generated cell to save memory
+        print('        cellname delete ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
+
+        print('    }', file=ofile)
+        print('}', file=ofile)
+
+        # Now create simple "fake" views of all the tiles.
+        print('gds readonly true', file=ofile)
+        print('gds rescale false', file=ofile)
+        print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
+        print('    for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
+        print('        set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
+        print('        set ylo [expr $ybase + $y * $stepheight]', file=ofile)
+        print('        set xhi [expr $xlo + $stepwidth]', file=ofile)
+        print('        set yhi [expr $ylo + $stepheight]', file=ofile)
+        print('        load ' + project_with_id + '_fill_pattern_${x}_$y -quiet', file=ofile)
+        print('        box values $xlo $ylo $xhi $yhi', file=ofile)
+        print('        paint comment', file=ofile)
+        print('        property FIXED_BBOX "$xlo $ylo $xhi $yhi"', file=ofile)
+        print('        property GDS_FILE ' + project_with_id + '_fill_pattern_${x}_${y}.gds', file=ofile)
+        print('        property GDS_START 0', file=ofile)
+        print('    }', file=ofile)
+        print('}', file=ofile)
+
+        # Now tile everything back together
+        print('load ' + project_with_id + '_fill_pattern -quiet', file=ofile)
+        print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
+        print('    for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
+        print('        box values 0 0 0 0', file=ofile)
+        print('        getcell ' + project_with_id + '_fill_pattern_${x}_$y child 0 0', file=ofile)
+        print('    }', file=ofile)
+        print('}', file=ofile)
+
+        # And write final GDS
+        print('puts stdout "Writing final GDS"', file=ofile)
+
+        print('cif *hier write disable', file=ofile)
+        print('cif *array write disable', file=ofile)
+        if hasgdsdir:
+            print('gds write ../gds/' + project_with_id + '_fill_pattern.gds', file=ofile)
+        else:
+            print('gds write ' + project_with_id + '_fill_pattern.gds', file=ofile)
+        print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
+        print('puts stdout "Ended: $endtime"', file=ofile)
         print('quit -noprompt', file=ofile)
 
     myenv = os.environ.copy()
     myenv['MAGTYPE'] = 'mag'
 
-    mproc = subprocess.run(['magic', '-dnull', '-noconsole',
-		'-rcfile', rcfile, magdir + '/generate_fill.tcl'],
+    if not testmode:
+        # Diagnostic
+        # print('This script will generate file ' + project_with_id + '_fill_pattern.gds')
+        print('This script will generate files ' + project_with_id + '_fill_pattern_x_y.gds')
+        print('Now generating fill patterns.  This may take. . . quite. . . a while.', flush=True)
+        mproc = subprocess.run(['magic', '-dnull', '-noconsole',
+		'-rcfile', rcfile, magpath + '/generate_fill.tcl'],
 		stdin = subprocess.DEVNULL,
 		stdout = subprocess.PIPE,
 		stderr = subprocess.PIPE,
-		cwd = magdir,
+		cwd = magpath,
 		env = myenv,
 		universal_newlines = True)
-    if mproc.stdout:
-        for line in mproc.stdout.splitlines():
-            print(line)
-    if mproc.stderr:
-        print('Error message output from magic:')
-        for line in mproc.stderr.splitlines():
-            print(line)
-        if mproc.returncode != 0:
-            print('ERROR:  Magic exited with status ' + str(mproc.returncode))
+        if mproc.stdout:
+            for line in mproc.stdout.splitlines():
+                print(line)
+        if mproc.stderr:
+            print('Error message output from magic:')
+            for line in mproc.stderr.splitlines():
+                print(line)
+            if mproc.returncode != 0:
+                print('ERROR:  Magic exited with status ' + str(mproc.returncode))
 
     if not keepmode:
-        os.remove(magdir + '/generate_fill.tcl')
+        # Remove fill generation script
+        os.remove(magpath + '/generate_fill.tcl')
+        # Remove all individual fill tiles, leaving only the composite GDS.
+        filelist = os.listdir(magpath)
+        for file in filelist:
+            if os.path.splitext(magpath + '/' + file)[1] == '.gds':
+                if file.startswith(project + '_fill_pattern_'):
+                    os.remove(magpath + '/' + file)
 
+    print('Done!')
     exit(0)
diff --git a/scripts/set_user_id.py b/scripts/set_user_id.py
index 3975354..8633ccb 100755
--- a/scripts/set_user_id.py
+++ b/scripts/set_user_id.py
@@ -1,4 +1,4 @@
-#!/bin/env python3
+#!/usr/bin/env python3
 # SPDX-FileCopyrightText: 2020 Efabless Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +14,7 @@
 # limitations under the License.
 # SPDX-License-Identifier: Apache-2.0
 
+#----------------------------------------------------------------------
 #
 # set_user_id.py ---
 #
@@ -21,7 +22,9 @@
 # user_id_programming block to set the user ID number.
 #
 # The user ID number is a 32-bit value that is passed to this routine
-# as an integer.
+# as an 8-digit hex number.  If not given as an option, then the script
+# will look for the value of the key "project_id" in the info.yaml file
+# in the project top level directory
 #
 # user_id_programming layout map:
 # Positions marked (in microns) for value = 0.  For value = 1, move
@@ -64,14 +67,22 @@
 # mask_rev[29]	27.83 20.23
 # mask_rev[30]	16.33 23.29
 # mask_rev[31]	 8.05 14.79
-#--------------------------------
+#----------------------------------------------------------------------
 
 import os
 import sys
 import re
 
 def usage():
-    print("set_user_id.py <user_id_value> [<path_to_project>]")
+    print("Usage:")
+    print("set_user_id.py [<user_id_value>] [<path_to_project>]")
+    print("")
+    print("where:")
+    print("    <user_id_value>   is a character string of eight hex digits, and")
+    print("    <path_to_project> is the path to the project top level directory.")
+    print("")
+    print("  If <user_id_value> is not given, then it must exist in the info.yaml file.")
+    print("  If <path_to_project> is not given, then it is assumed to be the cwd.")
     return 0
 
 if __name__ == '__main__':
@@ -98,127 +109,233 @@
         else:
             arguments.append(option)
 
-    if len(arguments) != 1 and len(arguments) != 2:
-        if len(arguments) != 0:
-            print("Wrong number of arguments given to cleanup_unref.py.")
+    if len(arguments) > 2:
+        print("Wrong number of arguments given to set_user_id.py.")
         usage()
         sys.exit(0)
 
     if '-debug' in optionlist:
         debugmode = True
 
-    user_id_value = arguments[0]
+    user_id_value = None
+    user_project_path = None
 
-    # Convert to binary
-    user_id_bits = '{0:032b}'.format(int(user_id_value))
+    if len(arguments) > 0:
+        user_id_value = arguments[0]
 
-    if len(arguments) == 2:
+        # Convert to binary
+        try:
+            user_id_int = int('0x' + user_id_value, 0)
+            user_id_bits = '{0:032b}'.format(user_id_int)
+        except:
+            user_project_path = arguments[0]
+
+    if len(arguments) == 0:
+        user_project_path = os.getcwd()
+    elif len(arguments) == 2:
         user_project_path = arguments[1]
+    elif user_project_path == None:
+        user_project_path = arguments[0]
     else:
         user_project_path = os.getcwd()
 
+    if not os.path.isdir(user_project_path):
+        print('Error:  Project path "' + user_project_path + '" does not exist or is not readable.')
+        sys.exit(1)
+
+    # Check for valid directories
+
+    if not user_id_value:
+        if os.path.isfile(user_project_path + '/info.yaml'):
+            with open(user_project_path + '/info.yaml', 'r') as ifile:
+                infolines = ifile.read().splitlines()
+                for line in infolines:
+                    kvpair = line.split(':')
+                    if len(kvpair) == 2:
+                        key = kvpair[0].strip()
+                        value = kvpair[1].strip()
+                        if key == 'project_id':
+                            user_id_value = value.strip('"\'')
+                            break
+
+            if not user_id_value:
+                print('Error:  No project_id key:value pair found in project info.yaml.')
+                sys.exit(1)
+
+            try:
+                user_id_int = int('0x' + user_id_value, 0)
+                user_id_bits = '{0:032b}'.format(user_id_int)
+            except:
+                print('Error:  Cannot parse user ID "' + user_id_value + '" as an 8-digit hex number.')
+                sys.exit(1)
+
+        else:
+            print('Error:  No info.yaml file and no user ID argument given.')
+            sys.exit(1)
+
+    print('Setting project user ID to: ' + user_id_value)
+
     magpath = user_project_path + '/mag'
     gdspath = user_project_path + '/gds'
     vpath = user_project_path + '/verilog'
     errors = 0 
 
-    if os.path.isdir(gdspath):
-
-        # Bytes leading up to via position are:
-        viarec = "00 06 0d 02 00 43 00 06 0e 02 00 2c 00 2c 10 03 "
-        viabytes = bytes.fromhex(viarec)
-
-        # Read the GDS file.  If a backup was made of the zero-value
-        # program, then use it.
-
-        gdsbak = gdspath + '/user_id_prog_zero.gds'
-        gdsfile = gdspath + '/user_id_programming.gds'
-
-        if os.path.isfile(gdsbak):
-            with open(gdsbak, 'rb') as ifile:
-                gdsdata = ifile.read()
-        else:
-            with open(gdsfile, 'rb') as ifile:
-                gdsdata = ifile.read()
-
-        for i in range(0,32):
-            # Ignore any zero bits.
-            if user_id_bits[i] == '0':
-                continue
-
-            coords = mask_rev[i]
-            xum = coords[0]
-            yum = coords[1]
-
-            # Contact is 0.17 x 0.17, so add and subtract 0.085 to get
-            # the corner positions.
-
-            xllum = xum - 0.085
-            yllum = yum - 0.085
-            xurum = xum + 0.085
-            yurum = yum + 0.085
- 
-            # Get the 4-byte hex values for the corner coordinates
-            xllnm = round(xllum * 1000)
-            yllnm = round(yllum * 1000)
-            xllhex = '{0:08x}'.format(xllnm)
-            yllhex = '{0:08x}'.format(yllnm)
-            xurnm = round(xurum * 1000)
-            yurnm = round(yurum * 1000)
-            xurhex = '{0:08x}'.format(xurnm)
-            yurhex = '{0:08x}'.format(yurnm)
-
-            # Magic's GDS output for vias always starts at the lower left
-            # corner and goes counterclockwise, repeating the first point.
-            viaoldposdata = viarec + xllhex + yllhex + xurhex + yllhex
-            viaoldposdata += xurhex + yurhex + xllhex + yurhex + xllhex + yllhex
-            
-            # For "one" bits, the X position is moved 0.92 microns to the left
-            newxllum = xllum - 0.92
-            newxurum = xurum - 0.92
-
-            # Get the 4-byte hex values for the new corner coordinates
-            newxllnm = round(newxllum * 1000)
-            newxllhex = '{0:08x}'.format(newxllnm)
-            newxurnm = round(newxurum * 1000)
-            newxurhex = '{0:08x}'.format(newxurnm)
-
-            vianewposdata = viarec + newxllhex + yllhex + newxurhex + yllhex
-            vianewposdata += newxurhex + yurhex + newxllhex + yurhex + newxllhex + yllhex
-
-            # Diagnostic
-            if debugmode:
-                print('Bit ' + str(i) + ':')
-                print('Via position ({0:3.2f}, {1:3.2f}) to ({2:3.2f}, {3:3.2f})'.format(xllum, yllum, xurum, yurum))
-                print('Old hex string = ' + viaoldposdata)
-                print('New hex string = ' + vianewposdata)
-
-            # Convert hex strings to byte arrays
-            viaoldbytedata = bytearray.fromhex(viaoldposdata)
-            vianewbytedata = bytearray.fromhex(vianewposdata)
-
-            # Replace the old data with the new
-            if viaoldbytedata not in gdsdata:
-                print('Error: via not found for bit position ' + str(i))
-                errors += 1 
-            else:
-                gdsdata = gdsdata.replace(viaoldbytedata, vianewbytedata)
-
-        if errors == 0:
-            # Keep a copy of the original 
-            if not os.path.isfile(gdsbak):
-                os.rename(gdsfile, gdsbak)
-
-            with open(gdsfile, 'wb') as ofile:
-                ofile.write(gdsdata)
-
-            print('Done!')
-            
-        else:
-            print('There were errors in processing.  No file written.')
-            sys.exit(1)
-
-    else:
-        print('No directory ' + gdspath + ' found.')
+    if not os.path.isdir(gdspath):
+        print('No directory ' + gdspath + ' found (path to GDS).')
         sys.exit(1)
+
+    if not os.path.isdir(vpath):
+        print('No directory ' + vpath + ' found (path to verilog).')
+        sys.exit(1)
+
+    if not os.path.isdir(magpath):
+        print('No directory ' + magpath + ' found (path to magic databases).')
+        sys.exit(1)
+
+    print('Step 1:  Modify GDS of the user_id_programming subcell')
+
+    # Bytes leading up to via position are:
+    viarec = "00 06 0d 02 00 43 00 06 0e 02 00 2c 00 2c 10 03 "
+    viabytes = bytes.fromhex(viarec)
+
+    # Read the GDS file.  If a backup was made of the zero-value
+    # program, then use it.
+
+    gdsbak = gdspath + '/user_id_prog_zero.gds'
+    gdsfile = gdspath + '/user_id_programming.gds'
+
+    if os.path.isfile(gdsbak):
+        with open(gdsbak, 'rb') as ifile:
+            gdsdata = ifile.read()
+    else:
+        with open(gdsfile, 'rb') as ifile:
+            gdsdata = ifile.read()
+
+    for i in range(0,32):
+        # Ignore any zero bits.
+        if user_id_bits[i] == '0':
+            continue
+
+        coords = mask_rev[i]
+        xum = coords[0]
+        yum = coords[1]
+
+        # Contact is 0.17 x 0.17, so add and subtract 0.085 to get
+        # the corner positions.
+
+        xllum = xum - 0.085
+        yllum = yum - 0.085
+        xurum = xum + 0.085
+        yurum = yum + 0.085
+ 
+        # Get the 4-byte hex values for the corner coordinates
+        xllnm = round(xllum * 1000)
+        yllnm = round(yllum * 1000)
+        xllhex = '{0:08x}'.format(xllnm)
+        yllhex = '{0:08x}'.format(yllnm)
+        xurnm = round(xurum * 1000)
+        yurnm = round(yurum * 1000)
+        xurhex = '{0:08x}'.format(xurnm)
+        yurhex = '{0:08x}'.format(yurnm)
+
+        # Magic's GDS output for vias always starts at the lower left
+        # corner and goes counterclockwise, repeating the first point.
+        viaoldposdata = viarec + xllhex + yllhex + xurhex + yllhex
+        viaoldposdata += xurhex + yurhex + xllhex + yurhex + xllhex + yllhex
+            
+        # For "one" bits, the X position is moved 0.92 microns to the left
+        newxllum = xllum - 0.92
+        newxurum = xurum - 0.92
+
+        # Get the 4-byte hex values for the new corner coordinates
+        newxllnm = round(newxllum * 1000)
+        newxllhex = '{0:08x}'.format(newxllnm)
+        newxurnm = round(newxurum * 1000)
+        newxurhex = '{0:08x}'.format(newxurnm)
+
+        vianewposdata = viarec + newxllhex + yllhex + newxurhex + yllhex
+        vianewposdata += newxurhex + yurhex + newxllhex + yurhex + newxllhex + yllhex
+
+        # Diagnostic
+        if debugmode:
+            print('Bit ' + str(i) + ':')
+            print('Via position ({0:3.2f}, {1:3.2f}) to ({2:3.2f}, {3:3.2f})'.format(xllum, yllum, xurum, yurum))
+            print('Old hex string = ' + viaoldposdata)
+            print('New hex string = ' + vianewposdata)
+
+        # Convert hex strings to byte arrays
+        viaoldbytedata = bytearray.fromhex(viaoldposdata)
+        vianewbytedata = bytearray.fromhex(vianewposdata)
+
+        # Replace the old data with the new
+        if viaoldbytedata not in gdsdata:
+            print('Error: via not found for bit position ' + str(i))
+            errors += 1 
+        else:
+            gdsdata = gdsdata.replace(viaoldbytedata, vianewbytedata)
+
+    if errors == 0:
+        # Keep a copy of the original 
+        if not os.path.isfile(gdsbak):
+            os.rename(gdsfile, gdsbak)
+
+        with open(gdsfile, 'wb') as ofile:
+            ofile.write(gdsdata)
+
+        print('Done!')
+            
+    else:
+        print('There were errors in processing.  No file written.')
+        print('Ending process.')
+        sys.exit(1)
+
+    print('Step 2:  Add user project ID parameter to verilog.')
+
+    changed = False
+    with open(vpath + '/rtl/caravel.v', 'r') as ifile:
+        vlines = ifile.read().splitlines()
+        outlines = []
+        for line in vlines:
+            oline = re.sub("parameter USER_PROJECT_ID = 32'h[0-9A-F]+;",
+			"parameter USER_PROJECT_ID = 32'h" + user_id_value + ";",
+			line)
+            if oline != line:
+                changed = True
+            outlines.append(oline)
+
+    if changed:
+        with open(vpath + '/rtl/caravel.v', 'w') as ofile:
+            for line in outlines:
+                print(line, file=ofile)
+            print('Done!')
+    else:
+        print('Error:  No substitutions done on verilog/rtl/caravel.v.')
+        print('Ending process.')
+        sys.exit(1)
+
+    print('Step 3:  Add user project ID text to top level layout.')
+
+    with open(magpath + '/user_id_textblock.mag', 'r') as ifile:
+        maglines = ifile.read().splitlines()
+        outlines = []
+        digit = 0
+        for line in maglines:
+            if 'alphaX_' in line:
+                dchar = user_id_value[digit].upper()
+                oline = re.sub('alpha_[0-9A-F]', 'alpha_' + dchar, line)
+                outlines.append(oline)
+                digit += 1
+            else:
+                outlines.append(line)
+
+    if digit == 8:
+        with open(magpath + '/user_id_textblock.mag', 'w') as ofile:
+            for line in outlines:
+                print(line, file=ofile)
+        print('Done!')
+    elif digit == 0:
+        print('Error:  No digits were replaced in the layout.')
+    else:
+        print('Error:  Only ' + str(digit) + ' digits were replaced in the layout.')
+
     sys.exit(0)