PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/FFdecsa/logic/logic.c

https://bitbucket.org/a84/gs_public/
C | 330 lines | 267 code | 38 blank | 25 comment | 20 complexity | c9b793359cc4c44843915ae6e8eeb27c MD5 | raw file
  1/* logic -- synthetize logic functions with 4 inputs
  2 *
  3 * Copyright (C) 2003-2004  fatih89r
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License as published by
  7 * the Free Software Foundation; either version 2 of the License, or
  8 * (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 18 */
 19
 20
 21
 22
 23/* Can we use negated inputs? */
 24#define noNEGATEDTOO
 25
 26
 27#include <stdio.h>
 28
 29
 30/*
 31 * abcd
 32 */
 33
 34#define BINARY(b15,b14,b13,b12,b11,b10,b9,b8,b7,b6,b5,b4,b3,b2,b1,b0) \
 35  ((b15)<<15)|((b14)<<14)|((b13)<<13)|((b12)<<12)| \
 36  ((b11)<<11)|((b10)<<10)|((b9) << 9)|((b8) << 8)| \
 37  ((b7) << 7)|((b6) << 6)|((b5) << 5)|((b4) << 4)| \
 38  ((b3) << 3)|((b2) << 2)|((b1) << 1)|((b0) << 0)
 39
 40struct fun{
 41  int level;
 42  int op_type;
 43  int op1;
 44  int op2;
 45};
 46
 47struct fun db[65536];
 48int n_fun;
 49
 50#define LEVEL_ALOT 1000000
 51
 52#define OP_FALSE 0
 53#define OP_TRUE  1
 54#define OP_SRC   2
 55#define OP_AND   3
 56#define OP_OR    4
 57#define OP_XOR   5
 58
 59#define SRC_A 10
 60#define SRC_B 20
 61#define SRC_C 30
 62#define SRC_D 40
 63#define SRC_AN 11
 64#define SRC_BN 21
 65#define SRC_CN 31
 66#define SRC_DN 41
 67
 68void dump_element_prefix(int);
 69void dump_element_infix(int);
 70
 71int main(void){
 72  int i,j;
 73  int l,p1,p2;
 74  int candidate;
 75  int max_p2_lev;
 76  
 77  for(i=0;i<65536;i++){
 78    db[i].level=LEVEL_ALOT;
 79  }
 80  n_fun=0;
 81
 82  db[0].level=0;
 83  db[0].op_type=OP_FALSE;
 84  n_fun++;
 85
 86  db[65535].level=0;
 87  db[65535].op_type=OP_TRUE;
 88  n_fun++;
 89
 90  db[BINARY(0,0,0,0, 0,0,0,0,  1,1,1,1, 1,1,1,1)].level=0;
 91  db[BINARY(0,0,0,0, 0,0,0,0,  1,1,1,1, 1,1,1,1)].op_type=OP_SRC;
 92  db[BINARY(0,0,0,0, 0,0,0,0,  1,1,1,1, 1,1,1,1)].op1=SRC_A;
 93  n_fun++;
 94
 95  db[BINARY(0,0,0,0, 1,1,1,1,  0,0,0,0, 1,1,1,1)].level=0;
 96  db[BINARY(0,0,0,0, 1,1,1,1,  0,0,0,0, 1,1,1,1)].op_type=OP_SRC;
 97  db[BINARY(0,0,0,0, 1,1,1,1,  0,0,0,0, 1,1,1,1)].op1=SRC_B;
 98  n_fun++;
 99
100  db[BINARY(0,0,1,1, 0,0,1,1,  0,0,1,1, 0,0,1,1)].level=0;
101  db[BINARY(0,0,1,1, 0,0,1,1,  0,0,1,1, 0,0,1,1)].op_type=OP_SRC;
102  db[BINARY(0,0,1,1, 0,0,1,1,  0,0,1,1, 0,0,1,1)].op1=SRC_C;
103  n_fun++;
104
105  db[BINARY(0,1,0,1, 0,1,0,1,  0,1,0,1, 0,1,0,1)].level=0;
106  db[BINARY(0,1,0,1, 0,1,0,1,  0,1,0,1, 0,1,0,1)].op_type=OP_SRC;
107  db[BINARY(0,1,0,1, 0,1,0,1,  0,1,0,1, 0,1,0,1)].op1=SRC_D;
108  n_fun++;
109#ifdef NEGATEDTOO
110  db[BINARY(1,1,1,1, 1,1,1,1,  0,0,0,0, 0,0,0,0)].level=0;
111  db[BINARY(1,1,1,1, 1,1,1,1,  0,0,0,0, 0,0,0,0)].op_type=OP_SRC;
112  db[BINARY(1,1,1,1, 1,1,1,1,  0,0,0,0, 0,0,0,0)].op1=SRC_AN;
113  n_fun++;
114
115  db[BINARY(1,1,1,1, 0,0,0,0,  1,1,1,1, 0,0,0,0)].level=0;
116  db[BINARY(1,1,1,1, 0,0,0,0,  1,1,1,1, 0,0,0,0)].op_type=OP_SRC;
117  db[BINARY(1,1,1,1, 0,0,0,0,  1,1,1,1, 0,0,0,0)].op1=SRC_BN;
118  n_fun++;
119
120  db[BINARY(1,1,0,0, 1,1,0,0,  1,1,0,0, 1,1,0,0)].level=0;
121  db[BINARY(1,1,0,0, 1,1,0,0,  1,1,0,0, 1,1,0,0)].op_type=OP_SRC;
122  db[BINARY(1,1,0,0, 1,1,0,0,  1,1,0,0, 1,1,0,0)].op1=SRC_CN;
123  n_fun++;
124
125  db[BINARY(1,0,1,0, 1,0,1,0,  1,0,1,0, 1,0,1,0)].level=0;
126  db[BINARY(1,0,1,0, 1,0,1,0,  1,0,1,0, 1,0,1,0)].op_type=OP_SRC;
127  db[BINARY(1,0,1,0, 1,0,1,0,  1,0,1,0, 1,0,1,0)].op1=SRC_DN;
128  n_fun++;
129#endif
130
131  for(l=0;l<100;l++){
132    printf("calculating level %i\n",l);
133    for(p1=1;p1<65536;p1++){
134      if(db[p1].level==LEVEL_ALOT) continue;
135      max_p2_lev=l-db[p1].level-1;
136      for(p2=p1+1;p2<65536;p2++){
137        if(db[p2].level>max_p2_lev) continue;
138
139        candidate=p1&p2;
140        if(db[candidate].level==LEVEL_ALOT){
141          //found new
142          db[candidate].level=db[p1].level+db[p2].level+1;
143          db[candidate].op_type=OP_AND;
144          db[candidate].op1=p1;
145          db[candidate].op2=p2;
146          n_fun++;
147	}
148
149        candidate=p1|p2;
150        if(db[candidate].level==LEVEL_ALOT){
151          //found new
152          db[candidate].level=db[p1].level+db[p2].level+1;
153          db[candidate].op_type=OP_OR;
154          db[candidate].op1=p1;
155          db[candidate].op2=p2;
156          n_fun++;
157	}
158
159        candidate=p1^p2;
160        if(db[candidate].level==LEVEL_ALOT){
161          //found new
162          db[candidate].level=db[p1].level+db[p2].level+1;
163          db[candidate].op_type=OP_XOR;
164          db[candidate].op1=p1;
165          db[candidate].op2=p2;
166          n_fun++;
167	}
168
169      }
170    }
171    printf("num fun=%i\n\n",n_fun);
172    fflush(stdout);
173    if(n_fun>=65536) break;
174  }
175
176
177  for(i=0;i<65536;i++){
178    if(db[i].level==LEVEL_ALOT) continue;
179
180    printf("PREFIX ");
181    for(j=15;j>=0;j--){
182      printf("%i",i&(1<<j)?1:0);
183      if(j%4==0) printf(" ");
184      if(j%8==0) printf(" ");
185    }
186    printf(" : lev %2i: ",db[i].level);
187    dump_element_prefix(i);
188    printf("\n");
189
190    printf("INFIX  ");
191    for(j=15;j>=0;j--){
192      printf("%i",i&(1<<j)?1:0);
193      if(j%4==0) printf(" ");
194      if(j%8==0) printf(" ");
195    }
196    printf(" : lev %2i: ",db[i].level);
197    dump_element_infix(i);
198    printf("\n");
199  }
200  
201  return 0;
202}
203
204void dump_element_prefix(int e){
205  if(db[e].level==LEVEL_ALOT){
206    printf("PANIC!\n");
207    return;
208  };
209  switch(db[e].op_type){
210  case OP_FALSE:
211    printf("0");
212    break;
213  case OP_TRUE:
214    printf("1");
215    break;
216  case OP_SRC:
217    switch(db[e].op1){
218    case SRC_A:
219      printf("a");
220      break;
221    case SRC_B:
222      printf("b");
223      break;
224    case SRC_C:
225      printf("c");
226      break;
227    case SRC_D:
228      printf("d");
229      break;
230    case SRC_AN:
231      printf("an");
232      break;
233    case SRC_BN:
234      printf("bn");
235      break;
236    case SRC_CN:
237      printf("cn");
238      break;
239    case SRC_DN:
240      printf("dn");
241      break;
242    }
243    break;
244  case OP_AND:
245    printf("FFAND(");
246    dump_element_prefix(db[e].op1);
247    printf(",");
248    dump_element_prefix(db[e].op2);
249    printf(")");
250    break;
251  case OP_OR:
252    printf("FFOR(");
253    dump_element_prefix(db[e].op1);
254    printf(",");
255    dump_element_prefix(db[e].op2);
256    printf(")");
257    break;
258  case OP_XOR:
259    printf("FFXOR(");
260    dump_element_prefix(db[e].op1);
261    printf(",");
262    dump_element_prefix(db[e].op2);
263    printf(")");
264    break;
265  }
266}
267
268void dump_element_infix(int e){
269  if(db[e].level==LEVEL_ALOT){
270    printf("PANIC!\n");
271    return;
272  };
273  switch(db[e].op_type){
274  case OP_FALSE:
275    printf("0");
276    break;
277  case OP_TRUE:
278    printf("1");
279    break;
280  case OP_SRC:
281    switch(db[e].op1){
282    case SRC_A:
283      printf("a");
284      break;
285    case SRC_B:
286      printf("b");
287      break;
288    case SRC_C:
289      printf("c");
290      break;
291    case SRC_D:
292      printf("d");
293      break;
294    case SRC_AN:
295      printf("an");
296      break;
297    case SRC_BN:
298      printf("bn");
299      break;
300    case SRC_CN:
301      printf("cn");
302      break;
303    case SRC_DN:
304      printf("dn");
305      break;
306    }
307    break;
308  case OP_AND:
309    printf("( ");
310    dump_element_infix(db[e].op1);
311    printf("&");
312    dump_element_infix(db[e].op2);
313    printf(" )");
314    break;
315  case OP_OR:
316    printf("( ");
317    dump_element_infix(db[e].op1);
318    printf("|");
319    dump_element_infix(db[e].op2);
320    printf(" )");
321    break;
322  case OP_XOR:
323    printf("( ");
324    dump_element_infix(db[e].op1);
325    printf("^");
326    dump_element_infix(db[e].op2);
327    printf(" )");
328    break;
329  }
330}