1 /*
2  * Copyright (c) 2013 Stephen Williams (steve@icarus.com)
3  *
4  *    This source code is free software; you can redistribute it
5  *    and/or modify it in source code form under the terms of the GNU
6  *    General Public License as published by the Free Software
7  *    Foundation; either version 2 of the License, or (at your option)
8  *    any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "priv.h"
21 # include  "nex_data.h"
22 # include  <cassert>
23 
24 using namespace std;
25 
print_lpm_ff(FILE * fd,ivl_lpm_t net)26 int print_lpm_ff(FILE*fd, ivl_lpm_t net)
27 {
28       int errors = 0;
29 
30       ivl_nexus_t nex_q = ivl_lpm_q(net);
31       blif_nex_data_t*ned_q = blif_nex_data_t::get_nex_data(nex_q);
32 
33       ivl_nexus_t nex_d = ivl_lpm_data(net,0);
34       blif_nex_data_t*ned_d = blif_nex_data_t::get_nex_data(nex_d);
35 
36       ivl_nexus_t nex_c = ivl_lpm_clk(net);
37       blif_nex_data_t*ned_c = blif_nex_data_t::get_nex_data(nex_c);
38 
39       ivl_nexus_t nex_ce = ivl_lpm_enable(net);
40       blif_nex_data_t*ned_ce = nex_ce? blif_nex_data_t::get_nex_data(nex_ce) : 0;
41 
42       if (ivl_lpm_async_clr(net)) {
43 	    errors += 1;
44 	    fprintf(stderr, "%s:%u: sorry: blif: Asynchronous clear not implemented yet\n",
45 		    ivl_lpm_file(net), ivl_lpm_lineno(net));
46       }
47 
48       if (ivl_lpm_async_set(net)) {
49 	    errors += 1;
50 	    fprintf(stderr, "%s:%u: sorry: blif: Asynchronous set not implemented yet\n",
51 		    ivl_lpm_file(net), ivl_lpm_lineno(net));
52       }
53 
54       fprintf(fd, "# IVL_LPM_FF: width=%u, Q=%s, D=%s, C=%s, CE=%s\n",
55 	      ivl_lpm_width(net), ned_q->get_name(), ned_d->get_name(), ned_c->get_name(), ned_ce? ned_ce->get_name() : "N/A");
56 
57       if (ned_ce) {
58 	      // If there is a clock-enable, rewrite this in a form
59 	      // that blif can accept. Transform this:
60 	      //
61 	      //   always @(posedge C) if (CE) Q <= D;
62 	      //
63 	      // to this:
64 	      //
65 	      //   always @(posedge C) Q <= CE? D : Q;
66 	      //
67 	      // In ASIC-land, this is probably OK.
68 	    for (unsigned wid = 0 ; wid < ivl_lpm_width(net) ; wid += 1) {
69 		  fprintf(fd, ".names %s%s %s%s %s%s %s%s/EN\n"
70 			  "0-1 1\n"
71 			  "11- 1\n",
72 			  ned_ce->get_name(), ned_ce->get_name_index(0),
73 			  ned_d ->get_name(), ned_d ->get_name_index(wid),
74 			  ned_q ->get_name(), ned_q ->get_name_index(wid),
75 			  ned_d ->get_name(), ned_d ->get_name_index(wid));
76 
77 		  fprintf(fd, ".latch %s%s/EN %s%s re %s%s 3\n",
78 			  ned_d->get_name(), ned_d->get_name_index(wid),
79 			  ned_q->get_name(), ned_q->get_name_index(wid),
80 			  ned_c->get_name(), ned_c->get_name_index(0));
81 	    }
82       } else {
83 	    for (unsigned wid = 0 ; wid < ivl_lpm_width(net) ; wid += 1) {
84 		  fprintf(fd, ".latch %s%s %s%s re %s%s 3\n",
85 			  ned_d->get_name(), ned_d->get_name_index(wid),
86 			  ned_q->get_name(), ned_q->get_name_index(wid),
87 			  ned_c->get_name(), ned_c->get_name_index(0));
88 	    }
89       }
90 
91       return errors;
92 }
93