OpenCores
URL https://opencores.org/ocsvn/hwlu/hwlu/trunk

Subversion Repositories hwlu

[/] [hwlu/] [trunk/] [sw/] [common.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 kavi
/************************************************************/
2
/* Filename   : common.c                                    */
3
/* Description: Commonly used functions.                    */
4
/* Author     : Nikolaos Kavvadias, <nkavv@physics.auth.gr> */
5
/* Date       : Tuesday, 09/02/2010.                        */
6
/* Revision   : --                                          */
7
/************************************************************/
8
 
9
#include <stdio.h>
10
#include <stdlib.h>   
11
 
12
 
13
void print_vhdl_header_common(FILE *outfile)
14
{
15
  fprintf(outfile,"----                                                              ----\n");
16
  fprintf(outfile,"---- Author: Nikolaos Kavvadias                                   ----\n");
17
  fprintf(outfile,"----         nkavv@physics.auth.gr                                ----\n");
18
  fprintf(outfile,"----                                                              ----\n");
19
  fprintf(outfile,"----==============================================================----\n");
20
  fprintf(outfile,"----                                                              ----\n");
21
  fprintf(outfile,"---- Copyright (C) 2004-2010   Nikolaos Kavvadias                 ----\n");
22
  fprintf(outfile,"----                    nkavv@uop.gr                              ----\n");
23
  fprintf(outfile,"----                    nkavv@physics.auth.gr                     ----\n");
24
  fprintf(outfile,"----                    nikolaos.kavvadias@gmail.com              ----\n");
25
  fprintf(outfile,"----                                                              ----\n");
26
  fprintf(outfile,"---- This source file may be used and distributed without         ----\n");
27
  fprintf(outfile,"---- restriction provided that this copyright statement is not    ----\n");
28
  fprintf(outfile,"---- removed from the file and that any derivative work contains  ----\n");
29
  fprintf(outfile,"---- the original copyright notice and the associated disclaimer. ----\n");
30
  fprintf(outfile,"----                                                              ----\n");
31
  fprintf(outfile,"---- This source file is free software; you can redistribute it   ----\n");
32
  fprintf(outfile,"---- and/or modify it under the terms of the GNU Lesser General   ----\n");
33
  fprintf(outfile,"---- Public License as published by the Free Software Foundation; ----\n");
34
  fprintf(outfile,"---- either version 2.1 of the License, or (at your option) any   ----\n");
35
  fprintf(outfile,"---- later version.                                               ----\n");
36
  fprintf(outfile,"----                                                              ----\n");
37
  fprintf(outfile,"---- This source is distributed in the hope that it will be       ----\n");
38
  fprintf(outfile,"---- useful, but WITHOUT ANY WARRANTY; without even the implied   ----\n");
39
  fprintf(outfile,"---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ----\n");
40
  fprintf(outfile,"---- PURPOSE. See the GNU Lesser General Public License for more  ----\n");
41
  fprintf(outfile,"---- details.                                                     ----\n");
42
  fprintf(outfile,"----                                                              ----\n");
43
  fprintf(outfile,"---- You should have received a copy of the GNU Lesser General    ----\n");
44
  fprintf(outfile,"---- Public License along with this source; if not, download it   ----\n");
45
  fprintf(outfile,"---- from <http://www.opencores.org/lgpl.shtml>                   ----\n");
46
  fprintf(outfile,"----                                                              ----\n");
47
  fprintf(outfile,"----==============================================================----\n");
48
  fprintf(outfile,"--\n");
49
  fprintf(outfile,"-- CVS Revision History\n");
50
  fprintf(outfile,"--\n");
51
  fprintf(outfile,"\n");
52
}
53
 
54
unsigned ipow(unsigned x, unsigned y)
55
{
56
  unsigned i;
57
  unsigned result;
58
 
59
  result = 1;
60
 
61
  for (i=1; i<=y; i++)
62
    result = result*x;
63
 
64
  return result;
65
}
66
 
67
unsigned dectobin(unsigned bin_data, int num_bits)
68
{
69
   int count;
70
   unsigned MASK;
71
   unsigned result;
72
   unsigned result_arr[100];
73
 
74
   count = num_bits;
75
   MASK = 1<<(count-1);
76
 
77
   result = 0;
78
 
79
   for (count=num_bits-1; count>-1; count--)
80
   {
81
     result_arr[count] = (( bin_data & MASK ) ? 1 : 0 );
82
     bin_data <<= 1;
83
   }
84
 
85
   for (count=num_bits-1; count>-1; count--)
86
     result = ipow(10,count)*result_arr[count] + result;
87
 
88
   return result;
89
}
90
 
91
/* log2 function for integers: unsigned log2(unsigned operand) */
92
unsigned log2(unsigned operand)
93
{
94
  unsigned temp;
95
  unsigned log_val;
96
 
97
  temp = operand-1;
98
  //temp = operand;
99
  log_val = 0;
100
 
101
  while (temp > 0)
102
  {
103
    temp = temp/2;
104
    log_val = log_val + 1;
105
  }
106
 
107
  return log_val;
108
}
109
 
110
void print_binary_value(FILE *outfile, int i, int bw)
111
{
112
  // Print integer value
113
  switch (bw)
114
  {
115
    case 1:
116
      fprintf(outfile,"%d",dectobin( i, bw ));
117
      break;
118
    case 2:
119
      fprintf(outfile,"%02d",dectobin( i, bw ));
120
      break;
121
    case 3:
122
      fprintf(outfile,"%03d",dectobin( i, bw ));
123
      break;
124
    case 4:
125
      fprintf(outfile,"%04d",dectobin( i, bw ));
126
      break;
127
    case 5:
128
      fprintf(outfile,"%05d",dectobin( i, bw ));
129
      break;
130
    case 6:
131
      fprintf(outfile,"%06d",dectobin( i, bw ));
132
      break;
133
    case 7:
134
      fprintf(outfile,"%07d",dectobin( i, bw ));
135
      break;
136
    case 8:
137
      fprintf(outfile,"%08d",dectobin( i, bw ));
138
      break;
139
    case 9:
140
      fprintf(outfile,"%09d",dectobin( i, bw ));
141
      break;
142
    case 10:
143
      fprintf(outfile,"%10d",dectobin( i, bw ));
144
      break;
145
    case 11:
146
      fprintf(outfile,"%11d",dectobin( i, bw ));
147
      break;
148
    case 12:
149
      fprintf(outfile,"%12d",dectobin( i, bw ));
150
      break;
151
    case 13:
152
      fprintf(outfile,"%13d",dectobin( i, bw ));
153
      break;
154
    case 14:
155
      fprintf(outfile,"%14d",dectobin( i, bw ));
156
      break;
157
    case 15:
158
      fprintf(outfile,"%15d",dectobin( i, bw ));
159
      break;
160
    case 16:
161
      fprintf(outfile,"%16d",dectobin( i, bw ));
162
      break;
163
    default:
164
      break;
165
  }
166
}
167
 
168
// Print binary value -- First bit is "one".
169
void print_binary_value_fbone(FILE *outfile, int i)
170
{
171
  // Print integer value
172
  switch ( log2(i) )
173
  {
174
    // i = 1
175
    case 0:
176
      fprintf(outfile,"%d",dectobin( i, i ));
177
      break;
178
    // i = 2:3
179
    case 1:
180
      fprintf(outfile,"%d",dectobin( i, log2(i) ));
181
      break;
182
    // i = 4:7 
183
    case 2:
184
      fprintf(outfile,"%02d",dectobin( i, log2(i) ));
185
      break;
186
    // i = 8:15
187
    case 3:
188
      fprintf(outfile,"%03d",dectobin( i, log2(i) ));
189
      break;
190
    // i = 16:31
191
    case 4:
192
      fprintf(outfile,"%04d",dectobin( i, log2(i) ));
193
      break;
194
    // i = 32:63
195
    case 5:
196
      fprintf(outfile,"%05d",dectobin( i, log2(i) ));
197
      break;
198
    default:
199
      break;
200
  }
201
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.