Updated copyright text/header in most source files.
[geda-gaf.git] / gnetlist / scheme / gnet-cascade.scm
blobcf5ece3f60ba3dea6889bcb0a106febdbb97f3d4
1 ;;; gEDA - GPL Electronic Design Automation
2 ;;; gnetlist - gEDA Netlist
3 ;;; Backend for cascade (http://rfcascade.sourceforge.net)
4 ;;; Copyright (C) 2003-2010 Dan McMahill
5 ;;;
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 2 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; if not, write to the Free Software
18 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Locate and print out the global defaults if the element exists
22 (define cascade:write-defaults-top
23   (lambda (port pkgs)
24     (if (not (null? pkgs))
25         (let ( (pkg (car pkgs)) )
26           (if (string=? (get-device pkg) "cascade-defaults-top")
27               (begin 
28                 (display "# Initial global defaults\n" port)
29                 (display "defaults " port)
30                 (map (lambda (attrib)
31                        (let ((val (gnetlist:get-package-attribute pkg attrib)))
32                          (if (not (string=? val "unknown"))
33                              (display (string-append attrib "=" val " ") port)
34                              )
35                          )
36                        )
37                      (list "rin" "RIN" "rout" "ROUT" "rho" "RHO")
38                      )
39                 (newline port)
40                 (newline port)
41                 )
42               (cascade:write-defaults-top port (cdr pkgs))
43               )
44           )
45         )
46     )
47   )
49 ;; Locate and print out the "source" line and return the refdes of
50 ;; the first element in the cascade
51 (define cascade:write-source
52   (lambda (port pkgs)
53     (if (not (null? pkgs))
54         (let ( (package (car pkgs) )
55                (sourcenet #f)
56                )
57           (if (string=? (get-device package) "cascade-source")
58               (begin
59                 (set! sourcenet (gnetlist:get-nets package "1"))
60                 (display "source " port)
61                 (map (lambda (attrib)
62                        (let ((val (gnetlist:get-package-attribute package attrib)))
63                          (if (not (string=? val "unknown"))
64                              (display (string-append attrib "=" val " ") port)
65                              )
66                          )
67                        )
68                      (list "c" "C" "cn0" "CN0" "cn" "CN" "bw" "BW")
69                      )
70                 (newline port)
72                 (if (string=? (caadr sourcenet) package)
73                     (caaddr sourcenet)
74                     (caadr sourcenet)
75                     )
76                 )
77               (cascade:write-source port (cdr pkgs) )
78               )
79           )
80         ;; the list of packages is now empty
81         '()
82         )
83     )
84   )
86 ;; recursively follow the cascade and print out each element as its
87 ;; found 
88 (define cascade:follow-cascade
89   (lambda (port pkg)
90     (if (not (null? pkg)) 
91         (begin
92           (let ( (outnet (gnetlist:get-nets pkg "2"))
93                  )
95             ;; Is this a "defaults" element or a normal element?
96             ;; If its a defaults element, then print "defaults"
97             ;; instead of the reference designator because thats
98             ;; a keyword for cascade.
99             (if (string=? (get-device pkg) "cascade-defaults")
100                 (display "defaults " port)
101                 (display (string-append pkg " ") port)
102                 )
104             ;; spit out all the relevant attributes for element or
105             ;; defaults lines
106             (map (lambda (attrib)
107                    (let ((val (gnetlist:get-package-attribute pkg attrib)))
108                      (if (not (string=? val "unknown"))
109                          (display (string-append attrib "=" val " ") port)
110                          )
111                      )
112                    )
113                  (list "g" "G" "gp" "GP" "gv" "GV" "nf" "NF" "iip3"
114                        "IIP3" "r" "R" "rin" "RIN" "rout" "ROUT"
115                        "rho" "RHO")
116                  )
117             (newline port)
119             ;;(display "cascade:follow-cascade  -- outnet = ")
120             ;;(display outnet)
121             ;;(newline)
122             (if (>= (length outnet) 3)
123                 (if (string=? (caadr outnet) pkg)
124                     (cascade:follow-cascade port(caaddr outnet))
125                     (cascade:follow-cascade port (caadr outnet))
126                     )
127                 )
128             )
129           )
130         )
131     )
132   )
134 ;; The top level netlister for cascade
135 (define cascade 
136    (lambda (filename)
137      (newline)
138      (display "---------------------------------\n")
139      (display "gEDA/gnetlist Cascade Backend\n")
141      (display "---------------------------------\n\n")
143      (display (string-append "Writing to output file \"" filename
144                              "\"... ") )
145       (let ((port (open-output-file filename))
146             (first_block #f)
147             )
149         ;; write the header
150         (display "# Cascade (http://rfcascade.sourceforge.net)\n"
151                  port)
152         (display "# Created with gEDA/gnetlist\n\n" port)
154         ;; Write out an initial "defaults" line if it exists
155         (cascade:write-defaults-top port packages)
157         ;; Write out the "source" line and keep track of what its
158         ;; connected to.  If we couldn't find the source, then
159         ;; exit out.
160         (display "# Source definition\n" port)
161         (set! first_block (cascade:write-source port packages))
162         (if (null? first_block)
163             (error "You must include a source element in your schematic!")
164             )
166         ;; write the components
167         (display "\n# Cascaded system\n" port)
168         (cascade:follow-cascade port first_block)
170         ;; write the footer
171         (newline port)
172         (display "# End of netlist created by gEDA/gnetlist\n\n" port)
173         
174         ;; close netlist
175         (close-output-port port)
177         )
178       
179       (display "done\n")
180       )
181    )