Guest User

Logic Minimization

a guest
Aug 21st, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int i,j,temp,NumberOfVariable,NumberOfAllMinterm,NumberOfDontCare,NumberOfEPI=0,
  5. NumberOfRemainingMT,NumberOfRemainingPI,NumberOfPI=0,PotEPINo=0,NumberOfPossibleEPI=
  6. 1,MinimumNo=0,Groupable=1;
  7. int *MintermIndicesDecimal, *MintermIndicesDecimal_DontCare, **Minterm_Binary,
  8. ****Column, **PI_Index,
  9. **EPI_Index,*NumberCounter,*ReducedPIChart_X,**ReducedPIChart_Y,**ReducedPIChart,
  10. *For, **Potential_EPI,*NoOfPIForEPI;
  11. void DecimalToBinary();
  12. int OneCounter(int *binary, int NumberOfDigit);
  13. int Combination(int n, int ColumnNo, int k);
  14. int IsPowerOfTwo(int n);
  15. int IsDontCare(int MT);
  16. void ShowResult();
  17. void Recursion_For_Loop(int m);
  18. int main()
  19. {
  20. int k,l,m,n,x,y,LogicProbe;
  21. /***********Preparation. Collect the information for the boolean
  22. expression***********/
  23. printf("Please provide the information for the sum of minterms.\n\nHow many
  24. variables does it contain?\n");
  25. scanf("%d",&NumberOfVariable);
  26. while(NumberOfVariable<=0)
  27. {
  28. printf("The number of variables should be greater than 0, please enter
  29. again:\n\n");
  30. printf("Please provide the information for the sum of minterms.\n\nHow
  31. many variables does it contain?\n");
  32. scanf("%d",&NumberOfVariable);
  33. }
  34. printf("How many minterms (including Don't-Care minterms) does it contain?\n");
  35. scanf("%d",&NumberOfAllMinterm);
  36. while(NumberOfAllMinterm>pow(2,NumberOfVariable) || NumberOfAllMinterm<=0)
  37. {
  38. printf("The number of minterms cannot be greater than 2^%d nor smaller
  39. than 1, please enter again:\n",NumberOfVariable);
  40. printf("How many minterms (including Don't-Care minterms) does it
  41. contain?\n");
  42. scanf("%d",&NumberOfAllMinterm);
  43. }
  44. printf("How many Don't-Care minterms does it contain?\n");
  45. scanf("%d",&NumberOfDontCare);
  46. while(NumberOfDontCare>=NumberOfAllMinterm || NumberOfDontCare<0)
  47. {
  48. printf("The number of Don't-Care minterms cannot be greater than the
  49. number of all minterms nor smaller than 0, please enter again:\n");
  50. printf("How many Don't-Care minterms does it contain?\n");
  51. scanf("%d",&NumberOfDontCare);
  52. }
  53. MintermIndicesDecimal=(int *)malloc(NumberOfAllMinterm*sizeof(int));
  54. /* Record the decimal indices representing each minterm */
  55. MintermIndicesDecimal_DontCare=(int *)malloc(NumberOfDontCare*sizeof(int));
  56. /* Record the decimal indices representing Don't-Care minterms */
  57. for(i=0;i<NumberOfAllMinterm;i++)
  58. {
  59. if(i==0)
  60. printf("Please enter the decimal index of the 1st minterm(in
  61. ascending order):");
  62. else if(i==1)
  63. printf("Please enter the decimal index of the 2nd minterm(in
  64. ascending order):");
  65. else if(i==2)
  66. printf("Please enter the decimal index of the 3rd minterm(in
  67. ascending order):");
  68. else
  69. printf("Please enter the decimal index of the %dth minterm(in
  70. ascending order):",i+1);
  71. scanf("%d",&MintermIndicesDecimal[i]);
  72. if(i!=0 && MintermIndicesDecimal[i]<=MintermIndicesDecimal[i-1])
  73. {
  74. printf("The numbers are not in ascending order, please re-enter
  75. all the indices again.\n\n");
  76. i=-1;
  77. }
  78. else if(MintermIndicesDecimal[i]>=pow(2,NumberOfVariable))
  79. {
  80. printf("\nThe number should be smaller than %d, please re-enter
  81. all the indices again.\n\n",pow(2,NumberOfVariable));
  82. i=-1;
  83. }
  84. }
  85. if(NumberOfDontCare!=0)
  86. {
  87. printf("\n\nWhich of them are Don't-Care terms?\n\n");
  88. for(i=0;i<NumberOfDontCare;i++)
  89. {
  90. if(i==0)
  91. printf("Please enter the decimal index of the 1st Don't-
  92. Care minterm (in ascending order):");
  93. else if(i==1)
  94. printf("Please enter the decimal index of the 2nd Don't-
  95. Care minterm (in ascending order):");
  96. else if(i==2)
  97. printf("Please enter the decimal index of the 3rd Don't-
  98. Care minterm (in ascending order):");
  99. else
  100. printf("Please enter the decimal index of the %dth Don't-
  101. Care minterm (in ascending order):",i+1);
  102. scanf("%d",&MintermIndicesDecimal_DontCare[i]);
  103. if(i!=0 &&
  104. MintermIndicesDecimal_DontCare[i]<=MintermIndicesDecimal_DontCare[i-1])
  105. {
  106. printf("The numbers are not in ascending order, please
  107. re-enter all the indices again.\n\n");
  108. i=-1;
  109. }
  110. else if(MintermIndicesDecimal[i]>=pow(2,NumberOfVariable))
  111. {
  112. printf("\nThe number should be smaller than %d, please
  113. re-enter all the indices again.\n\n",pow(2,NumberOfVariable));
  114. i=-1;
  115. }
  116. }
  117. }
  118. /***********Transform the decimal indices into Binary format and obtain relative
  119. information.***********/
  120. Minterm_Binary=(int **)malloc(NumberOfAllMinterm*sizeof(int*));
  121. for(i=0;i<=NumberOfAllMinterm;i++)
  122. {
  123. Minterm_Binary[i]=(int *)malloc((NumberOfVariable+4)*sizeof(int)); //Create array
  124. }
  125. DecimalToBinary();
  126. for(i=0;i<NumberOfAllMinterm;i++)
  127. {
  128. Minterm_Binary[i][NumberOfVariable]=OneCounter(Minterm_Binary[i],NumberOfVariab
  129. le); //number of '1'
  130. Minterm_Binary[i][NumberOfVariable+1]=0;
  131. /* '0' means it hasn't been grouped, '1' means it has been
  132. grouped with other terms */
  133. Minterm_Binary[i][NumberOfVariable+2]=MintermIndicesDecimal[i];
  134. /* this is its original minterm */
  135. Minterm_Binary[i][NumberOfVariable+3]=MintermIndicesDecimal[i];
  136. /* this is all the minterms it consists of */
  137. }
  138. /***********Prepare the first column for grouping***********/
  139. Column=(int ****)malloc((NumberOfVariable+1)*sizeof(int***));
  140. for(i=0;i<NumberOfVariable+1;i++)
  141. {
  142. Column[i]=(int ***)malloc((NumberOfVariable+1-i)*sizeof(int**));
  143. /* Column[i] contains all the terms in the (i+1)th column
  144. */
  145. }
  146. for(i=0;i<NumberOfVariable+1;i++) //i is column index, j is number of '1', k is element
  147. {
  148. for(j=0;j<NumberOfVariable+1-i;j++)
  149. {
  150. Column[i][j]=(int
  151. **)malloc(Combination(NumberOfVariable,i,j)*sizeof(int*)); /* Column[i][j]
  152. contains all the terms with j '1's in their binary indices in the (i+1)th column */
  153. for(k=0;k<Combination(NumberOfVariable,i,j);k++)
  154. {
  155. Column[i][j][k]=NULL;
  156. /* Column[i][j][k]
  157. represents a term with in the j '1's in their binary indices in the (i+1)th column
  158. */
  159. }
  160. }
  161. }
  162. for(i=0;i<NumberOfVariable+1;i++)
  163. {
  164. for(j=0,k=0;j<NumberOfAllMinterm;j++)
  165. {
  166. if(Minterm_Binary[j][NumberOfVariable]==i)
  167. {
  168. Column[0][i][k++]=Minterm_Binary[j];
  169. /* Prepare the first grouping column */
  170. }
  171. }
  172. }
  173. /***********Perform the grouping***********/
  174. for(i=0;i<NumberOfVariable+1;i++)
  175. {
  176. if(Groupable)
  177. {
  178. Groupable=0;
  179. for(j=0;j<NumberOfVariable-i;j++)
  180. {
  181. int p,position;
  182. m=0;
  183. for(k=0;k<Combination(NumberOfVariable,i,j);k++)
  184. if(Column[i][j][k]!=NULL)
  185. {
  186. for(l=0;l<Combination(NumberOfVariable,i,j+1);l++)
  187. {
  188. if(Column[i][j+1][l]!=NULL
  189. && Column[i][j+1][l][NumberOfVariable+2+i]>Column[i][j][k][NumberOfVariable+2+i] &&
  190. IsPowerOfTwo(Column[i][j+1][l][NumberOfVariable+2+i]-
  191. Column[i][j][k][NumberOfVariable+2+i])) //If the element of the next group is bigger 2^n, then the difference is only 1
  192. {
  193. LogicProbe=0-i;
  194. /*This LogicProbe is used to check whether this two terms has the same positions of
  195. '-'(which is represented by '2')*/
  196. for(n=1;n<=i;n++)
  197. for(p=1;p<=i;p++)
  198. if(Column[i][j+1][l][NumberOfVariable+1+n]==Column[i][j][k][NumberOfVariable+1+
  199. p])
  200. {
  201. LogicProbe++;
  202. }
  203. if(LogicProbe==0)
  204. {
  205. Groupable=1;
  206. Column[i][j][k][NumberOfVariable+1]=1; //Mark paired
  207. Column[i][j+1][l][NumberOfVariable+1]=1; //Mark paired
  208. Column[i+1][j][m]=(int *)malloc((NumberOfVariable+4+i+pow(2,i+1))*sizeof(int)); //Create next column
  209. for(n=0;n<=NumberOfVariable+1+i;n++)
  210. {
  211. Column[i+1][j][m][n]=Column[i][j][k][n];
  212. }
  213. Column[i+1][j][m][NumberOfVariable+3+i]=Column[i][j][k][NumberOfVariable+2+i];
  214. for(n=NumberOfVariable+4+i;n<NumberOfVariable+4+i+pow(2,i+1);n++)
  215. Column[i+1][j][m][n]=0;
  216. position=log((Column[i][j+1][l][NumberOfVariable+2+i]-
  217. Column[i][j][k][NumberOfVariable+2+i]))/log(2);
  218. Column[i+1][j][m][NumberOfVariable-1-position]=2;
  219. Column[i+1][j][m][NumberOfVariable+1]=0;
  220. Column[i+1][j][m][NumberOfVariable+2+i]=position;
  221. for(p=0;p<pow(2,i);p++)
  222. {
  223. Column[i+1][j][m][NumberOfVariable+4+i+p]=Column[i][j][k][NumberOfVariable+3+i+
  224. p];
  225. }
  226. for(p=pow(2,i);p<pow(2,i+1);p++)
  227. {
  228. Column[i+1][j][m][NumberOfVariable+4+i+p]=Column[i][j+1][l][NumberOfVariable+3+
  229. i+p-(int)pow(2,i)];
  230. }
  231. m++;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. /***********NumberCounter count how many times each decimal index occurs***********/
  240. NumberCounter=(int *)malloc(pow(2,NumberOfVariable)*sizeof(int));
  241. for(i=0;i<pow(2,NumberOfVariable);i++)
  242. NumberCounter[i]=0;
  243. /***********Record the Prime Implicants(duplicates will be removed)***********/
  244. PI_Index=(int **)malloc(NumberOfAllMinterm*sizeof(int*));
  245. for(i=0;i<NumberOfAllMinterm;i++)
  246. {
  247. PI_Index[i]=(int *)malloc(3*sizeof(int));
  248. }
  249. for(i=0;i<NumberOfVariable+1;i++)
  250. for(j=0;j<NumberOfVariable+1-i;j++)
  251. for(k=0;k<Combination(NumberOfVariable,i,j);k++)
  252. {
  253. if(Column[i][j][k]!=NULL &&
  254. Column[i][j][k][NumberOfVariable+1]==0)
  255. {
  256. LogicProbe=0-pow(2,i); /*LogicProbe is used to
  257. check whether this PI is a duplicate*/
  258. for(l=k-1;l>=0;l--)
  259. if(LogicProbe!=0)
  260. {
  261. LogicProbe=0-pow(2,i);
  262. for(m=0;m<pow(2,i);m++)
  263. for(n=0;n<pow(2,i);n++)
  264. if(Column[i][j][l][NumberOfVariable+3+i+m]==Column[i][j][k][NumberOfVariable+3+
  265. i+n])
  266. {
  267. LogicProbe++;
  268. }
  269. }
  270. if(LogicProbe!=0)
  271. {
  272. PI_Index[NumberOfPI][0]=i;
  273. PI_Index[NumberOfPI][1]=j;
  274. PI_Index[NumberOfPI][2]=k;
  275. NumberOfPI++;
  276. for(l=0;l<pow(2,i);l++)
  277. {
  278. NumberCounter[Column[i][j][k][NumberOfVariable+3+i+l]]++;
  279. }
  280. }
  281. }
  282. }
  283. /***********Remove the DontCare minterms***********/
  284. for(i=0;i<NumberOfDontCare;i++)
  285. NumberCounter[MintermIndicesDecimal_DontCare[i]]=0;
  286. EPI_Index=(int **)malloc(NumberOfAllMinterm*sizeof(int*));
  287. /***********In the PI Chart, find the minterms which only occurs once, and select
  288. the PIs which contain these minterms as EPIs and record them. Then set NumberCounter
  289. of this minterms to 0***********/
  290. for(i=0;i<pow(2,NumberOfVariable);i++)
  291. if(NumberCounter[i]==1)
  292. for(j=0;j<NumberOfPI;j++)
  293. for(k=0;k<pow(2,PI_Index[j][0]);k++)
  294. {
  295. if(Column[PI_Index[j][0]][PI_Index[j][1]][PI_Index[j][2]][NumberOfVariable+3+PI
  296. _Index[j][0]+k]==i)
  297. {
  298. EPI_Index[NumberOfEPI]=PI_Index[j];
  299. for(l=0;l<pow(2,PI_Index[j][0]);l++)
  300. NumberCounter[Column[PI_Index[j][0]][PI_Index[j][1]][PI_Index[j][2]][NumberOfVa
  301. riable+3+PI_Index[j][0]+l]]=0;
  302. NumberOfEPI++;
  303. k=pow(2,PI_Index[j][0]);
  304. }
  305. }
  306. /***********Make the Reduced PI Chart***********/
  307. NumberOfRemainingMT=0;
  308. for(i=0;i<pow(2,NumberOfVariable);i++)
  309. if(NumberCounter[i]!=0)
  310. NumberOfRemainingMT++;
  311. ReducedPIChart_X=(int *)malloc(NumberOfRemainingMT*sizeof(int));
  312. for(i=0;i<NumberOfRemainingMT;i++)
  313. ReducedPIChart_X[i]=-1;
  314. ReducedPIChart_Y=(int **)malloc(NumberOfPI*sizeof(int*));
  315. for(i=0;i<NumberOfPI;i++)
  316. ReducedPIChart_Y[i]=NULL;
  317. ReducedPIChart=(int **)malloc(NumberOfRemainingMT*sizeof(int*));
  318. /***********This is the First Row, consist of the remaining minterms decimal
  319. indices***********/
  320. for(i=0,j=0;j<pow(2,NumberOfVariable);j++)
  321. if(NumberCounter[j]!=0)
  322. {
  323. ReducedPIChart_X[i]=j;
  324. i++;
  325. }
  326. /***********This is the First Column, consist of the remaining PIs***********/
  327. NumberOfRemainingPI=0;
  328. for(i=0;i<NumberOfPI;i++)
  329. for(j=0;j<pow(2,PI_Index[i][0]);j++)
  330. {
  331. if(NumberCounter[Column[PI_Index[i][0]][PI_Index[i][1]][PI_Index[i][2]][NumberO
  332. fVariable+3+PI_Index[i][0]+j]]!=0)
  333. {
  334. j=pow(2,PI_Index[i][0]);
  335. ReducedPIChart_Y[NumberOfRemainingPI]=PI_Index[i];
  336. NumberOfRemainingPI++;
  337. }
  338. }
  339. /***********ReducedPIChart[i][j] represent the information of Reduced PI Chart('1'
  340. means picked, '0' means unpicked)***********/
  341. if(NumberOfRemainingPI!=0)
  342. {
  343. for(i=0;i<NumberOfRemainingMT;i++)
  344. ReducedPIChart[i]=(int *)malloc(NumberOfRemainingPI*sizeof(int));
  345. for(i=0;i<NumberOfRemainingMT;i++)
  346. for(j=0;j<NumberOfRemainingPI;j++)
  347. ReducedPIChart[i][j]=0;
  348. for(i=0;i<NumberOfRemainingPI;i++)
  349. for(j=0;j<pow(2,ReducedPIChart_Y[i][0]);j++)
  350. for(k=0;k<NumberOfRemainingMT;k++)
  351. if(Column[ReducedPIChart_Y[i][0]][ReducedPIChart_Y[i][1]][ReducedPIChart_Y[i][2
  352. ]][NumberOfVariable+3+ReducedPIChart_Y[i][0]+j]==ReducedPIChart_X[k])
  353. {
  354. ReducedPIChart[k][i]=1;
  355. }
  356. /***********Select the EPIs from the Reduced PI Chart***********/
  357. For=(int *)malloc(NumberOfRemainingMT*sizeof(int)); /* For[i] will be
  358. used in the function 'Recursion_For_Loop(int m)' */
  359. for(i=0;i<NumberOfRemainingMT;i++)
  360. {
  361. For[i]=-1;
  362. }
  363. for(i=0;i<NumberOfRemainingMT;i++)
  364. NumberOfPossibleEPI=NumberOfPossibleEPI*NumberCounter[ReducedPIChart_X[i]];
  365. Potential_EPI=(int **)malloc(NumberOfPossibleEPI*sizeof(int*));
  366. for(i=0;i<NumberOfPossibleEPI;i++)
  367. {
  368. Potential_EPI[i]=(int *)malloc(NumberOfRemainingMT*sizeof(int));
  369. }
  370. Recursion_For_Loop(NumberOfRemainingMT-1);
  371. NoOfPIForEPI=(int *)malloc(NumberOfPossibleEPI*sizeof(int)); /*
  372. NoOfPIForEPI[i] will count how many PIs are in each combination that covers all
  373. minterms */
  374. for(i=0;i<NumberOfPossibleEPI;i++)
  375. NoOfPIForEPI[i]=0;
  376. for(i=0;i<NumberOfPossibleEPI;i++)
  377. for(j=0;j<NumberOfRemainingMT;j++)
  378. if(Potential_EPI[i][j]!=-1)
  379. {
  380. NoOfPIForEPI[i]++;
  381. for(k=j+1;k<NumberOfRemainingMT;k++)
  382. if(Potential_EPI[i][k]==Potential_EPI[i][j])
  383. Potential_EPI[i][k]=-1;
  384. }
  385. /***********Find the combination which require the least number of PIs to cover all
  386. minterms***********/
  387. for(i=1;i<NumberOfPossibleEPI;i++)
  388. if(NoOfPIForEPI[i]<NoOfPIForEPI[MinimumNo])
  389. MinimumNo=i;
  390. for(i=0;i<NumberOfRemainingMT;i++)
  391. if(Potential_EPI[MinimumNo][i]!=-1)
  392. EPI_Index[NumberOfEPI++]=ReducedPIChart_Y[Potential_EPI[MinimumNo][i]];
  393. /***********Print the final result of minimal SOP expression***********/
  394. printf("\nThe simplified SOP expression is:\n\n");
  395. printf("\n ");
  396. for(x=0;x<NumberOfEPI;x++)
  397. {
  398. for(y=0;y<NumberOfVariable;y++)
  399. {
  400. if(Column[EPI_Index[x][0]][EPI_Index[x][1]][EPI_Index[x][2]][y]==1)
  401. printf("%c",65+y);
  402. else
  403. if(Column[EPI_Index[x][0]][EPI_Index[x][1]][EPI_Index[x][2]][y]==0)
  404. printf("%c'",65+y);
  405. }
  406. if(x<NumberOfEPI-1)
  407. printf("+");
  408. }
  409. printf("\n\nPress any key to exit...");
  410. scanf("%d",&i);
  411. return 0;
  412. }
  413. else
  414. {
  415. printf("\n\nThe simplified SOP expression is:\n\n");
  416. printf("\n ");
  417. for(x=0;x<NumberOfEPI;x++)
  418. {
  419. for(y=0;y<NumberOfVariable;y++)
  420. {
  421. if(Column[EPI_Index[x][0]][EPI_Index[x][1]][EPI_Index[x][2]][y]==1)
  422. printf("%c",65+y);
  423. else
  424. if(Column[EPI_Index[x][0]][EPI_Index[x][1]][EPI_Index[x][2]][y]==0)
  425. printf("%c'",65+y);
  426. }
  427. if(x<NumberOfEPI-1)
  428. printf("+");
  429. }
  430. printf("\n\nPress any key to exit...");
  431. scanf("%d",&i);
  432. return 0;
  433. }
  434. }
  435. int IsDontCare(int MT)
  436. {
  437. int i;
  438. for(i=0;i<NumberOfDontCare;i++)
  439. if(MT==MintermIndicesDecimal_DontCare[i])
  440. return 1;
  441. return 0;
  442. }
  443. void DecimalToBinary() //Convert decimal to binary
  444. {
  445. int i,j,dividend;
  446. for(i=0;i<NumberOfAllMinterm;i++)
  447. {
  448. dividend=MintermIndicesDecimal[i];
  449. for(j=NumberOfVariable-1;j>=0;j--)
  450. {
  451. Minterm_Binary[i][j]=dividend%2;
  452. dividend=dividend/2;
  453. }
  454. }
  455. }
  456. int OneCounter(int *binary, int NumberOfDigit) //Count number of '1'
  457. {
  458. int i,count=0;
  459. for(i=0;i<=NumberOfDigit-1;i++)
  460. {
  461. if(binary[i]==1)
  462. count++;
  463. }
  464. return count;
  465. }
  466. int Combination(int n, int ColumnNo, int k) //Find combination
  467. {
  468. int Comb,i,NtoK=1,Kto1=1;
  469. for(i=n;i>=n-k+1-ColumnNo;i--)
  470. {
  471. NtoK=i*NtoK;
  472. }
  473. for(i=k;i>=1;i--)
  474. {
  475. Kto1=i*Kto1;
  476. }
  477. Comb=NtoK/Kto1;
  478. return Comb;
  479. }
  480. int IsPowerOfTwo(int n) //Check is a power of two
  481. {
  482. return(floor(log(n)/log(2))==(log(n)/log(2)));
  483. }
  484. void Recursion_For_Loop(int m) //Finding PI combination that includes all the remaining
  485. {
  486. int n=m;
  487. for(For[n]=0;For[n]<NumberOfRemainingPI;For[n]++)
  488. {
  489. if(ReducedPIChart[NumberOfRemainingMT-1-n][For[n]])
  490. {
  491. if(n>0)
  492. {
  493. m=n;
  494. m--;
  495. Recursion_For_Loop(m);
  496. }
  497. else if(n==0)
  498. {
  499. for(i=0;i<NumberOfRemainingMT;i++)
  500. {
  501. Potential_EPI[PotEPINo][i]=For[NumberOfRemainingMT-1-i];
  502. }
  503. PotEPINo++;
  504. }
  505. }
  506. }
  507. }
Advertisement
Add Comment
Please, Sign In to add comment