summaryrefslogtreecommitdiffstats
path: root/fpga/xilinx/programmer/bit2svf/bitshandle.c
blob: 7fb2f40621e8a793f7f405e2bd6e301c85091f7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 /**[txh]********************************************************************
                                                                                
  Copyright (c) 2005 Juan Pablo D. Borgna <jpborgna en inti gov ar>
  Copyright (c) 2006-2007 Salvador E. Tropea <salvador en inti gov ar>
  Copyright (c) 2005-2007 Instituto Nacional de Tecnología Industrial
  
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA

  Description: Low level functions to output hexadecimal bitstreams.
                                                                                
***************************************************************************/    
/*****************************************************************************  
                                                                                
 Target:      Any                                                               
 Language:    C
 Compiler:    gcc 3.3.5 (Debian GNU/Linux)
 Text editor: SETEdit 0.5.5                                                     
                                                                                
*****************************************************************************/  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "bitshandle.h"

/* This function returns the inverted bits of its argument */
unsigned char
inv_byte(unsigned char b)
{
 int i;
 unsigned char t=0;

 for (i=0;i<7;i++)
    {
     t|= (b & 0x01);
     t = t << 1;
     b = b >> 1;
    }
 t|=b & 0x01;
 return t;
}

/* This function is used to generate a string with the hex value of the
   inverted bits of from */
void
pbi(char *to, char *from,long nbytes)
{
 int i;
 int j;
 char t[]="ZZ";
 
 if (to==NULL) return;

 j=nbytes*2;
 
 for (i=0;i<nbytes;i++)
    {
     sprintf(t,"%02x",inv_byte(from[i]));
     j-=2;
     to[j]=t[0];
     to[j+1]=t[1];
    }
}

/* This function is used to generate a string with the hex value of the
   bits of from */
void
pb(char *to, char *from,long nbytes)
{
 int i;
 int j;
 char t[]="ZZ";
 
 if (to==NULL) return;

 j=0;
 
 for (i=0;i<nbytes;i++)
    {
     sprintf(t,"%02x",from[i]);
     j+=2;
     to[j]=t[0];
     to[j+1]=t[1];
    }
}

/*this function writes bytes to a file with a max nuber of
  chars per line */ /* it should not be here */
int
cutputs(char *string, FILE *fp, long block, int *col)
{
 long remaining;
 long wrote=0l;
 int c=*col;

 remaining=strlen(string);
 if (!cutlines)
   {
    fwrite(string,1,remaining,fp);
    col+=remaining;
    return 0;
   }
 if (remaining<block)
    block=remaining;

 while (remaining)
      {
       long to_write=block-c;
       if (to_write<0) to_write=0;
       if (to_write & 1) to_write++;
       wrote=fwrite(string,1,to_write,fp);
       if (wrote!=to_write)
         {
          perror("CUTPUTS");
          return 1;
         }
       c+=wrote;
       string+=wrote;
       remaining-=wrote;
       if (remaining)
         {
          fputs("\n",fp);
          c=0;
          if (remaining<block)
             block=remaining;
         }
      }
 *col=c;
 return 0;
}