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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [beta_1.2/] [scripts/] [theia_compile] - Blame information for rev 112

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
$Line = 0;
24
%Registers;
25
%Instructions;
26
%Labels;
27
%EntryPoints;
28
@EntryKeys;
29
open OUT, ">Instructions.mem" or die "Can't open output file for R/W\n";
30
PopulateRegistersAndInstructionSet();
31
GenerateLabels();
32
ParseCode();
33
print
34
"
35
** Compilation successful! **
36
 
37
The output has been written to 'Code.mem' file.
38
Please make to copy this file under the same folder
39
where the simulation executable is, and edit the
40
'Creg.mem' let the RTL executable know that it
41
should use the shader.
42
Have a nice one!
43
 
44
";
45
close OUT;
46
exit(0);
47
 
48
 
49
#-------------------------------------------------------------------
50
sub PopulateRegistersAndInstructionSet()
51
{
52
   open DEFINITIONS, $DefsPath or die "'$DefsPath' : $!\n";
53
   while ()
54
   {
55
      #skip comments and empty lines
56
      next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
57
 
58
       if (m/`define\s*(ENTRYPOINT_ADRR_\S*)[\s|\t]+.*\'d(\d+)/)
59
       {
60
         push @EntryKeys, $1;
61
         $EntryPoints{$1}= sprintf('%08X',$2);
62
 
63
         next;
64
       }
65
 
66
       if (m/\`define\s*([C|O]REG\_\S*)[\s|\t]+.*\'d(\d+)/ ||
67
          m/\`define\s*([R|C]\d+)[\s|\t]+.*\'d(\d+)/)
68
       {
69
          $Registers{$1} = sprintf('%X', $2);
70
          next;
71
       }
72
 
73
       if (m/\`define\s*(\S*)[\s|\t]+\`INSTRUCTION_OP_LENGTH\'b([0|1|\_]+)/)
74
       {
75
       $Instructions{$1} = $2;
76
       $key = $1;
77
       #get rid of the '_' characters
78
       $Instructions{$key} =~ s/\_//g;
79
       #convert to hexadecimal
80
       $Instructions{$key}= sprintf('%X', oct("0b$Instructions{$key}"));
81
       next;
82
      }
83
      if (m/\`define\s*(\S*)[\s|\t]+32\'d(\d+)/)
84
      {
85
          $Registers{$1} = $2;
86
      }
87
 
88
 
89
   }
90
 
91
   $InstructionCount = 0;
92
   close DEFINITIONS;
93
   #Ok, add some extra stuff
94
   $Registers{'VOID'} = 0;
95
   $Registers{'RT_TRUE'} = 1;
96
   $Registers{'RT_FALSE'} = 0;
97
}
98
#-------------------------------------------------------------------
99
sub GenerateLabels()
100
{
101
 
102
   my $IP = 0;
103
   open CODE, $CodePath or die "'$CodePath': $!\n";
104
   while ()
105
   {
106
      #skip comments and empty lines
107
      next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
108
      $Line++;
109
      #print "$_ &&&& $IP\n";
110
 
111
      if (m/^(ENTRYPOINT_ADRR_\S+)\:.*\n/)
112
 
113
      {
114
         #print "$1\n";
115
         die "Error line $Line: Entry point not defined $1\n" if  (not defined $EntryPoints{$1});
116
          $EntryPoints{$1} = sprintf("%06X",$IP | 0x8000);#sprintf("%08X",$Line-1 | 0x8000);
117
          #print ">> $EntryPoints{$1}\n";
118
         next;
119
      }
120
 
121
       if (m/[\s|\t]*(\S+)\:.*\n/)
122
       {
123
           print "$1 " . sprintf("%08X",$InstructionCount | 0x8000) . "\n";
124
           $Labels{$1} = ($IP | 0x8000);#(($Line-1) | 0x8000); #sprintf("%08X",$Line-1 | 0x8000)
125
           print "$1 =  $Labels{$1}\n";
126
           next;
127
       }
128
 
129
      $IP++;
130
      $InstructionCount++;
131
   }
132
   close CODE;
133
   $Line = 0;
134
}
135
#-------------------------------------------------------------------
136
sub DecodeInstruction
137
{
138
   my ($Line,$EntryAddrCount,$OP,@Tokens) = @_;
139
   my @D_Tokens;
140
    if ( defined $Instructions{$OP}) {  $D_OP = hex $Instructions{$OP};
141
    } else {
142
       die "Error Line $Line: undefined instruction '$OP'\n";
143
    }
144
 
145
   foreach $Token (@Tokens)
146
   {
147
      next if ($Token eq '');
148
      if ($Token =~ m/(RT_TRUE|RT_FALSE)/)
149
      {
150
         push @D_Tokens,0;
151
         push @D_Tokens,0;
152
         push @D_Tokens,$Registers{$Token};
153
      } elsif ($Token =~ m/SWIZZLE.*/){
154
          push @D_Tokens,0;
155
          push @D_Tokens,$Registers{$Token};
156
      } elsif (defined  $Registers{$Token} ){
157
 
158
         push @D_Tokens,  hex $Registers{$Token};
159
 
160
      } elsif (defined  $Labels{$Token})
161
      {
162
         print "$Labels{$Token}\n";
163
         push @D_Tokens, ($Labels{$Token}+$EntryAddrCount);
164
      } elsif ($Token =~ m/32\'h(.*)/) {
165
         #if this is inmmediate value of 32 bits, then insert 0 and then the value
166
         push  @D_Tokens,0;
167
         push  @D_Tokens, hex $1;
168
      } else {
169
         die "Error Line $Line: undefined token '$Token'\n"
170
      }
171
   }
172
 
173
   return ($D_OP,@D_Tokens);
174
}
175
#-------------------------------------------------------------------
176
sub ParseCode()
177
{
178
 
179
my $ByteCount = ($InstructionCount+$#EntryKeys+1)*2;
180
print OUT "$ByteCount //Number of ioctects in file\n";
181
print OUT "//--------------------------------------\n";
182
print OUT "//Subroutine Entry Point Table\n";
183
 
184
my $IP = 0;
185
for my $k ( @EntryKeys )
186
{
187
   my $foo =  (oct("0x$EntryPoints{$k}") & 0x8000) ?
188
   sprintf("%08X", oct("0x$EntryPoints{$k}")+$#EntryKeys+1) :
189
   sprintf("%08X", oct("0x$EntryPoints{$k}")) ;
190
    print OUT "00000000 $foo\t//$k\n";
191
 
192
   $IP++;
193
}
194
 
195
print OUT "//--------------------------------------\n";
196
 
197
open CODE, $CodePath or die "$CodePath: $!\n";
198
while ()
199
{
200
      $Line++;
201
       #skip comments and empty lines
202
       next if (m/^([\s|\t]*\/\/)|^(\s*\n).*/);
203
       #skip labels
204
       next if (m/(\S+\:).*\n/);
205
       #get rid of comments in the same line
206
       s/\/\/.*\n/\n/g;
207
 
208
       my $OpcodeH,$OpcodeL;
209
       ($OP,$DST,$R1,$R2) = ("","","","");
210
      #Handle OP DST R1 R2
211
      if (m/^[\s|\t]*(\S+)\s+(\S+)\s*(\S*)\s*(\S*).*\n/)
212
      {
213
         ($OP,$DST,$R1,$R2) = ($1,$2,$3,$4);
214
         my @D = DecodeInstruction($Line,$#EntryKeys+1,$OP,($DST,$R1,$R2) );
215
 
216
 
217
         $OpcodeH = ($D[0] << 16) + $D[1];
218
         $OpcodeL = ($D[2] << 16) + $D[3];
219
      } else {
220
          die "Error: line $Line: incorrect statement '$_'\n";
221
      }
222
 
223
       printf OUT "%08X",$OpcodeH;
224
       printf OUT " %08X",$OpcodeL;
225
      print OUT "\t\t//  ".sprintf("IP %06X",$IP | 0x8000) . " ($IP): $OP $DST $R1 $R2\n"; $IP++;
226
 
227
 
228
}
229
 
230
close CLOSE;
231
}
232
#-------------------------------------------------------------------
233
 

powered by: WebSVN 2.1.0

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