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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [trunk/] [scripts/] [theia_compile] - Blame information for rev 142

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 67 diegovalve
#!/usr/bin/perl
2
################################################################
3
#Theia, Ray Cast Programable graphic Processing Unit.
4
#Copyright (C) 2010  Diego Valverde (diego.valverde.g@gmail.com)
5
#
6
#This program is free software; you can redistribute it and/or
7
#modify it under the terms of the GNU General Public License
8
#as published by the Free Software Foundation; either version 2
9
#of the License, or (at your option) any later version.
10
#
11
#This program is distributed in the hope that it will be useful,
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#GNU General Public License for more details.
15
#
16
#You should have received a copy of the GNU General Public #License
17
#along with this program; if not, write to the Free Software
18
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  #02110-1301, USA.
19
################################################################
20
$CodePath = $ARGV[0];
21
$DefsPath = "aDefinitions.v";
22
 
23 110 diegovalve
 
24
print
25
"
26
---------------------------------------------------------------
27
 
28
 _/_/_/_/_/  _/                  _/
29
   _/      _/_/_/      _/_/          _/_/_/
30
  _/      _/    _/  _/_/_/_/  _/  _/    _/
31
 _/      _/    _/  _/        _/  _/    _/
32
_/      _/    _/    _/_/_/  _/    _/_/_/
33
 
34
 
35
---------------------------------------------------------------
36
Compiling file '$CodePath'
37
 
38
 
39
";
40
 
41 67 diegovalve
$Line = 0;
42
%Registers;
43
%Instructions;
44
%Labels;
45
%EntryPoints;
46
@EntryKeys;
47 110 diegovalve
$OutputFile = "Instructions.mem";
48
open OUT, ">$OutputFile" or die "Can't open output file for R/W\n";
49 67 diegovalve
PopulateRegistersAndInstructionSet();
50
GenerateLabels();
51
ParseCode();
52
print
53
"
54
 
55 110 diegovalve
Output file: '$OutputFile'
56 67 diegovalve
 
57 110 diegovalve
** Compilation successful! **
58
---------------------------------------------------------------
59 67 diegovalve
";
60
close OUT;
61
exit(0);
62
 
63
 
64
#-------------------------------------------------------------------
65
sub PopulateRegistersAndInstructionSet()
66
{
67
   open DEFINITIONS, $DefsPath or die "'$DefsPath' : $!\n";
68
   while ()
69
   {
70
      #skip comments and empty lines
71
      next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
72
 
73
       if (m/`define\s*(ENTRYPOINT_ADRR_\S*)[\s|\t]+.*\'d(\d+)/)
74
       {
75
         push @EntryKeys, $1;
76
         $EntryPoints{$1}= sprintf('%08X',$2);
77
 
78
         next;
79
       }
80
 
81
       if (m/\`define\s*([C|O]REG\_\S*)[\s|\t]+.*\'d(\d+)/ ||
82
          m/\`define\s*([R|C]\d+)[\s|\t]+.*\'d(\d+)/)
83
       {
84
          $Registers{$1} = sprintf('%X', $2);
85
          next;
86
       }
87
 
88
       if (m/\`define\s*(\S*)[\s|\t]+\`INSTRUCTION_OP_LENGTH\'b([0|1|\_]+)/)
89
       {
90
       $Instructions{$1} = $2;
91
       $key = $1;
92
       #get rid of the '_' characters
93
       $Instructions{$key} =~ s/\_//g;
94
       #convert to hexadecimal
95
       $Instructions{$key}= sprintf('%X', oct("0b$Instructions{$key}"));
96
       next;
97
      }
98
      if (m/\`define\s*(\S*)[\s|\t]+32\'d(\d+)/)
99
      {
100
          $Registers{$1} = $2;
101
      }
102
 
103
 
104
   }
105
 
106
   $InstructionCount = 0;
107
   close DEFINITIONS;
108
   #Ok, add some extra stuff
109
   $Registers{'VOID'} = 0;
110
   $Registers{'RT_TRUE'} = 1;
111
   $Registers{'RT_FALSE'} = 0;
112
}
113
#-------------------------------------------------------------------
114
sub GenerateLabels()
115
{
116
 
117
   my $IP = 0;
118
   open CODE, $CodePath or die "'$CodePath': $!\n";
119
   while ()
120
   {
121
      #skip comments and empty lines
122
      next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
123
      $Line++;
124
      #print "$_ &&&& $IP\n";
125
 
126
      if (m/^(ENTRYPOINT_ADRR_\S+)\:.*\n/)
127
 
128
      {
129
         #print "$1\n";
130
         die "Error line $Line: Entry point not defined $1\n" if  (not defined $EntryPoints{$1});
131
          $EntryPoints{$1} = sprintf("%06X",$IP | 0x8000);#sprintf("%08X",$Line-1 | 0x8000);
132 110 diegovalve
          print "Implemented @ 0x$EntryPoints{$1} --> $1\n";
133 67 diegovalve
         next;
134
      }
135
 
136
       if (m/[\s|\t]*(\S+)\:.*\n/)
137
       {
138 110 diegovalve
          # print "$1 " . sprintf("%08X",$InstructionCount | 0x8000) . "\n";
139 67 diegovalve
           $Labels{$1} = ($IP | 0x8000);#(($Line-1) | 0x8000); #sprintf("%08X",$Line-1 | 0x8000)
140 110 diegovalve
         #  print "$1 =  $Labels{$1}\n";
141 67 diegovalve
           next;
142
       }
143
 
144
      $IP++;
145
      $InstructionCount++;
146
   }
147
   close CODE;
148
   $Line = 0;
149
}
150
#-------------------------------------------------------------------
151
sub DecodeInstruction
152
{
153
   my ($Line,$EntryAddrCount,$OP,@Tokens) = @_;
154
   my @D_Tokens;
155
    if ( defined $Instructions{$OP}) {  $D_OP = hex $Instructions{$OP};
156
    } else {
157
       die "Error Line $Line: undefined instruction '$OP'\n";
158
    }
159
 
160
   foreach $Token (@Tokens)
161
   {
162
      next if ($Token eq '');
163
      if ($Token =~ m/(RT_TRUE|RT_FALSE)/)
164
      {
165
         push @D_Tokens,0;
166
         push @D_Tokens,0;
167
         push @D_Tokens,$Registers{$Token};
168
      } elsif ($Token =~ m/SWIZZLE.*/){
169
          push @D_Tokens,0;
170
          push @D_Tokens,$Registers{$Token};
171
      } elsif (defined  $Registers{$Token} ){
172
 
173
         push @D_Tokens,  hex $Registers{$Token};
174
 
175
      } elsif (defined  $Labels{$Token})
176
      {
177 110 diegovalve
        # print "$Labels{$Token}\n";
178 67 diegovalve
         push @D_Tokens, ($Labels{$Token}+$EntryAddrCount);
179 110 diegovalve
          }elsif (defined  $EntryPoints{$Token}){
180
 
181
             push @D_Tokens,hex $EntryPoints{$Token};
182 67 diegovalve
      } elsif ($Token =~ m/32\'h(.*)/) {
183
         #if this is inmmediate value of 32 bits, then insert 0 and then the value
184
         push  @D_Tokens,0;
185
         push  @D_Tokens, hex $1;
186
      } else {
187
         die "Error Line $Line: undefined token '$Token'\n"
188
      }
189
   }
190
 
191
   return ($D_OP,@D_Tokens);
192
}
193
#-------------------------------------------------------------------
194
sub ParseCode()
195
{
196
 
197
my $ByteCount = ($InstructionCount+$#EntryKeys+1)*2;
198
print OUT "$ByteCount //Number of ioctects in file\n";
199
print OUT "//--------------------------------------\n";
200
print OUT "//Subroutine Entry Point Table\n";
201
 
202
my $IP = 0;
203
for my $k ( @EntryKeys )
204
{
205
   my $foo =  (oct("0x$EntryPoints{$k}") & 0x8000) ?
206
   sprintf("%08X", oct("0x$EntryPoints{$k}")+$#EntryKeys+1) :
207
   sprintf("%08X", oct("0x$EntryPoints{$k}")) ;
208
    print OUT "00000000 $foo\t//$k\n";
209
 
210
   $IP++;
211
}
212
 
213
print OUT "//--------------------------------------\n";
214
 
215
open CODE, $CodePath or die "$CodePath: $!\n";
216
while ()
217
{
218
      $Line++;
219
       #skip comments and empty lines
220
       next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
221
       #skip labels
222
       next if (m/(\S+\:).*\n/);
223
       #get rid of comments in the same line
224
       s/\/\/.*\n/\n/g;
225
 
226
       my $OpcodeH,$OpcodeL;
227
       ($OP,$DST,$R1,$R2) = ("","","","");
228
      #Handle OP DST R1 R2
229
      if (m/^[\s|\t]*(\S+)\s+(\S+)\s*(\S*)\s*(\S*).*\n/)
230
      {
231
         ($OP,$DST,$R1,$R2) = ($1,$2,$3,$4);
232
         my @D = DecodeInstruction($Line,$#EntryKeys+1,$OP,($DST,$R1,$R2) );
233
 
234
 
235
         $OpcodeH = ($D[0] << 16) + $D[1];
236
         $OpcodeL = ($D[2] << 16) + $D[3];
237
      } else {
238
          die "Error: line $Line: incorrect statement '$_'\n";
239
      }
240
 
241
       printf OUT "%08X",$OpcodeH;
242
       printf OUT " %08X",$OpcodeL;
243
      print OUT "\t\t//  ".sprintf("IP %06X",$IP | 0x8000) . " ($IP): $OP $DST $R1 $R2\n"; $IP++;
244
 
245
 
246
}
247
 
248
close CLOSE;
249
}
250
#-------------------------------------------------------------------
251
 

powered by: WebSVN 2.1.0

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