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

Subversion Repositories avuc

[/] [avuc/] [trunk/] [avuc.pl] - Blame information for rev 8

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

Line No. Rev Author Line
1 5 fblanco
#!/usr/bin/perl
2
 
3
#  Create a VHDL entity that implements a program written in a pseudo-assembler language
4
#  whose commands are user-defined in VHDL.
5
#
6
#  Copyright 2008 by Fernando Blanco <ferblanco@anagramix.com>
7
#
8
#  This source file may be used and distributed without
9
#  restriction provided that this copyright statement is not
10
#  removed from the file and that any derivative work contains
11
#  the original copyright notice and the associated disclaimer.
12
#
13
#  This source file is free software; you can redistribute it
14
#  and/or modify it under the terms of the GNU Lesser General
15
#  Public License as published by the Free Software Foundation;
16
#  either version 2.1 of the License, or (at your option) any
17
#  later version.
18
#
19
#  This source is distributed in the hope that it will be
20
#  useful, but WITHOUT ANY WARRANTY; without even the implied
21
#  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22
#  PURPOSE.  See the GNU Lesser General Public License for more
23
#  details.
24
#
25
#  You should have received a copy of the GNU Lesser General
26
#  Public License along with this source; if not, download it
27
#  from http://www.gnu.org/licenses/lgpl.html
28
 
29
 
30
$_indent = "   ";
31
$_command_key = "tryglspjw2u";
32 6 fblanco
$_line_end = "\n";
33 5 fblanco
 
34
@predefined_ucode = (
35
   "nop",
36
   "jump" );
37
 
38
%predefined_label = (
39
   "begin" => 0,
40
   "end" => 1);
41
 
42
use Getopt::Std;
43
use File::Basename;
44
 
45
$prog_name='avuc.pl';
46
$ver='1.0';
47
 
48
getopts( 'o:dvh', \%opt ) or usage();
49
usage() if $opt{h};
50
 
51
sub usage()
52
{
53
   print "$prog_name version $ver\n";
54
   print "Creates a VHDL file that runs a program based on assembler with VHDL user-defined commands\n";
55
   print "Usage: $prog_name [flags] infile\n";
56
   print "   -o   output filename\n";
57
   print "   -h   show this help\n";
58
   print "   -v   verbose\n";
59
   print "   -d   debugging information\n";
60
   exit;
61
}
62
 
63
$debug = 1 if $opt{d};
64
$verbose = 1 if ( $opt{v} || $opt{d} );
65
 
66
print "=====================================\n$prog_name version $ver\n" if ($verbose);
67
# Opening input file:
68
$ifile_name = $ARGV[0];
69
if ( !$ifile_name ) { usage() }
70
print "Input file:    $ifile_name\n" if $verbose;
71
open IFILE, "< $ifile_name" or die "\'$ifile_name\' cannot be opened\n$!";
72
 
73
# fileparse_set_fstype(MSWin32);
74
fileparse_set_fstype(Linux);
75
($base_ifile, $path_ifile, $type) = fileparse($ifile_name, '\.usm');
76
 
77
# Opening output file:
78
$ofile_name = $path_ifile . $base_ifile . '.vhd';
79
if ( $opt{o} ) {
80
   $ofile_name = $opt{o};
81
}
82
print "Output file:   $ofile_name\n" if $verbose;
83
open OFILE, "> $ofile_name" or die "\'$ofile_name\' cannot be opened\n$!";
84
 
85
################## usm definitions file reading ######################
86
@usm_ucode = @predefined_ucode;
87
$usm_cycle_bus_width = 1;
88
$usmc_error_no = 0;
89
$usmc_line_no = 0;
90
$read_next_line = "yes";
91
while (1) {
92
   if ( $read_next_line =~ /yes/ ) {
93
      $read_line = <IFILE>;
94
      if (eof) { &print_message_end_usm; }
95
      $usmc_line_no++;
96
   }
97
   $read_next_line = "yes";
98
   $_ = $read_line;
99
   split;
100
   $_ = $_[0];
101
   if (/^&\$(.*)/) {
102
      print "Line $usmc_line_no: Command '$1' found\n" if ($verbose);
103
      if ($1 =~ /^end_usm$/) {last;}
104
      $_ = '&rd_'.$1.'(@_);';
105
      @_ = eval $_;
106
#       print "$@" if ($@ );
107
      print "Line $usmc_line_no: Error: &\$"."$1 function not implemented\n" if ($@ );
108
      if ($_[0] =~ /^$_command_key$/) {
109
         $read_line = $_[1];
110
         $read_next_line = "no";
111
      }
112
   }
113
};
114
print "File read finished. $usmc_error_no errors found\n************************\n";
115
if ( $usmc_error_no ) { exit; }
116
 
117
######################### VHDL file writing ##########################
118
 
119
#Header, include & entity:
120
@time = localtime(time);
121
$time[4] += 1;
122
$time[5] += 1900;
123
print OFILE "-- File generated by $prog_name ($time[3]/$time[4]/$time[5])$_line_end--$_line_end";
124
print OFILE @usm_header;
125
print OFILE @usm_include;
126
&wr_entity;
127
 
128
# Architecture:
129
print OFILE "$_line_end$_line_end"."architecture program of $usm_entity is$_line_end$_line_end";
130
print OFILE @usm_sig_declaration, $_line_end;
131
 
132
$usm_opcode_bus_width = &bus_width( $#usm_ucode + keys %usm_jump_code );
133
# print "$usm_opcode_bus_width, $#usm_ucode"; exit;
134
$usm_pc_bus_width = &bus_width( $usmc_prog_line_no );
135
if ($usm_pc_bus_width > $usm_data_bus_width) { $usm_data_bus_width = $usm_pc_bus_width; }
136
$usm_opcode_format = "%0".$usm_opcode_bus_width."b";
137
$usm_pc_format = "%0".$usm_pc_bus_width."b";
138
$usm_data_format = "%0".$usm_data_bus_width."b";
139
&wr_sig_decl_breg;
140
&wr_sig_decl_clist;
141
&wr_sig_decl_llist;
142
&wr_sig_decl_ulist;
143
 
144
# Processes:
145
print OFILE "$_line_end"."begin$_line_end$_line_end";
146
print OFILE "--***************************** Program processes *****************************--$_line_end";
147
&wr_proc_init;
148
&wr_program;
149
print OFILE "--************************** Jump & cycle processes ***************************--$_line_end";
150
&wr_jump;
151
print OFILE "--**************************** Drivers processes ******************************--$_line_end";
152
&wr_drivers;
153
print OFILE "--******************************** Extra code *********************************--$_line_end";
154
print OFILE @usm_extra_code;
155
print OFILE "$_line_end"."end program;$_line_end";
156
exit;
157
 
158
##########################################################################
159
########################### Defined arrays ###############################
160
##########################################################################
161
# 
162
# %usm_opcode = (drivers_name_1, $uref_1, drivers_name_2, $uref_2, ... );
163
#    $uref_1 = {ucode_name_1 => $cref_1, ucode_name_2 => $cref_2, ...};
164
#       $cref_1 = {cycle_1 => $lref_1, cycle_2 => $lref_2, ...};
165
#          $lref_1 = [line_1, line_2 ...];
166
# 
167
# @usm_ucode = (@predefined_ucode, ucode_name_1, ucode_name_2, ...);
168
# 
169
# %usm_jump_code = (jump_code_name_1 => condition_1, jump_code_name_2 => condition_2, ...);
170
# 
171
# @usm_comment = (prog_line_0_comments, prog_line_1_comments, ...);
172
# 
173
# @usm_command = (prog_line_0_command, prog_line_1_command, ...);
174
# 
175
# @usm_data = (prog_line_0_data, prog_line_1_data, ...);
176
# 
177
# %usm_label = (label_name_0 => prog_line_label_0, label_name_1 => prog_line_label_1, ...};
178
# 
179
##################################################################################
180
################################# Subroutines ####################################
181
##################################################################################
182
 
183
# Calculates the necessary bus width to hold a vector with maximum value $_[0]: 
184
sub bus_width {
185
   use integer;
186
   my($i, $aux) = (0, $_[0]);
187
   while ($aux != 1) {
188
      $aux /= 2;
189
      ++$i;
190
   }
191
   return ++$i;
192
}
193
 
194
############################### Read subroutines #################################
195
 
196
# Reads the name of the main clock:
197
sub print_message_end_usm {
198
   print "Error: Ending command 'end_usm' not found\n";
199
   $usmc_error_no++;
200
   print "$usmc_error_no errors found\n************************\n";
201
   exit;
202
}
203
 
204
# Reads a line, returns 1 if the first word is a command, 0 if not:
205
sub line_first_word {
206
   my $remove_trail_blanks = $_[0];
207
   $read_line = <IFILE>;
208
   if (eof) { &print_message_end_usm; }
209
   $usmc_line_no++;
210
   $read_line =~ s/[$_line_end]+//;                   # remove end of line (Linux/DOS)
211
   if ( $remove_trail_blanks ) {
212
      $read_line =~ s/\s*(.*)/\1/;
213
   }
214
# print "$read_line#$1\n";
215
# if ( $usmc_line_no > 24 ) { exit; }
216
#    if ( $usmc_line_no > 90 && $usmc_line_no < 110 ) {
217
 
218
   $read_line = $read_line . $_line_end;
219
   @split_line = split (/\s+/, $read_line);
220
   $_ = $split_line[0];
221
   if (/^&\$(.*)/) {
222
      $read_command = $1;
223
      return 1;
224
   }
225
   return 0;
226
}
227
 
228
# Reads the name of the main clock:
229
sub rd_clock {
230
   $usm_main_clk = $_[1];
231
}
232
 
233
# Reads the data bus minimum length:
234
sub rd_data_bus_min_width {
235
   $usm_data_bus_width = $_[1];
236
}
237
 
238
# Reads the name of the entity to create:
239
sub rd_entity {
240
   $usm_entity = $_[1];
241
}
242
 
243
# Reads the output file header:
244
sub rd_header {
245
   while (1) {
246
      if ( &line_first_word(1) ) {
247
         return $_command_key, $read_line;
248
      }
249
      push(@usm_header, $read_line);
250
   }
251
}
252
 
253
# Reads the VHDL include text:
254
sub rd_include {
255
   while (1) {
256
      if ( &line_first_word(1) ) {
257
         return $_command_key, $read_line;
258
      }
259
      push(@usm_include, $read_line);
260
   }
261
}
262
 
263
# Reads the VHDL generic parameters:
264
sub rd_generic {
265
   while (1) {
266
      if ( &line_first_word(1) ) {
267
         return $_command_key, $read_line;
268
      }
269
      if ( $read_line !~ /^$_line_end+/ ) { push(@usm_generic, $read_line); }
270
   }
271
}
272
 
273
# Reads the VHDL port signals:
274
sub rd_port {
275
   while (1) {
276
      if ( &line_first_word(1) ) {
277
         return $_command_key, $read_line;
278
      }
279
      if ( $read_line !~ /^$_line_end+/ ) { push(@usm_port, $read_line); }
280
   }
281
}
282
 
283
# Reads the VHDL internal signals declarations:
284
sub rd_sig_declaration {
285
   while (1) {
286
      if ( &line_first_word(1) ) {
287
         return $_command_key, $read_line;
288
      }
289
      push(@usm_sig_declaration, $read_line);
290
   }
291
}
292
 
293
# Reads opcodes:
294
sub rd_opcode_def {
295
   my $opcode, $ucode, %cycles = (), %ucodes = ();
296
   $opcode = $_[1];
297
   while ( !&line_first_word(1) ) {}
298
   do {
299
      $ucode = $read_command;
300
      print "Line $usmc_line_no:    Action '$ucode' found\n" if ($verbose);
301
      if ( $ucode !~ /^default$/ ) {
302
         while ( !&line_first_word(1) ) {}
303
         if ( $read_command !~ /^cycle_def$/ ) {
304
            print "Line $usmc_line_no: Error: 'cycle_def' expected instead of '$read_command'\n";
305
            $usmc_error_no++;
306
         }
307
      }
308
      do {
309
         $cycle_def_no = $split_line[1];
310
#       print "$opcode-$ucode-$cycle_def_no\n";
311
         if ($cycle_def_no > $usm_cycle_bus_width) { $usm_cycle_bus_width = $cycle_def_no; }
312
         --$cycle_def_no;
313
         # Lines capture for this ucode:
314
         my @cycle_lines = ();
315
         while ( !&line_first_word(1) ) {
316
            push(@cycle_lines, $read_line);
317
         }
318
# print "WW @cycle_lines";
319
         if ( $ucode =~ /^default$/ ) {
320
            # default - VHDL lines:
321
            $ucodes{$ucode} = [ @cycle_lines ];
322
         } else {
323
            # Hash cycle number - VHDL lines:
324
            $cycles{$cycle_def_no} = [ @cycle_lines ];
325
            # ucode name - cycles hash:
326
            $ucodes{$ucode} = { %cycles };
327
         }
328
         # opcode name - ucodes hash:
329
         $usm_opcode{$opcode} = { %ucodes };
330
# print "^^ $usm_opcode{$opcode}{$ucode}{$cycle_def_no}[0]";
331
      } while ( $read_command =~ /^cycle_def$/ );
332
      # opcode list:
333
      push (@usm_ucode, $ucode);
334
   } while ( $read_command !~ /^end_opcode_def$/ );
335
}
336
 
337
# Reads jumps:
338
sub rd_jump_opcode_def {
339
   my $jump_type, $jump_type;
340
   while ( !&line_first_word(1) ) {}
341
   if ( $read_command !~ /^condition$/ ) {
342
      print "Line $usmc_line_no: Error: 'condition' expected instead of '$read_command'\n";
343
      $usmc_error_no++;
344
   }
345
   do {
346
      $jump_type = $split_line[1];
347
      print "Line $usmc_line_no:    Jump '$jump_type' found\n" if ($verbose);
348
      # Condition capture:
349
      $condition = "";
350
      while ( !&line_first_word(1) ) {
351
         $read_line =~ s/[$_line_end]+//;
352
         $condition = $condition . $read_line . " ";
353
      }
354
      $usm_jump_code{$jump_type} = $condition;
355
   } while ( $read_command =~ /^condition$/ );
356
   return $_command_key, $read_line;
357
}
358
 
359
# Reads the program:
360
sub rd_prog_code {
361
   my $command, $label, $prog_line_no = 0;
362
   @usm_comment = ();
363
   %usm_label = %predefined_label;
364
   while (1) {
365
      if ( &line_first_word(1) ) { last; }
366
# print "--$prog_line_no: $read_line--";
367
      if ($read_line =~ /^$_line_end/) { next; }
368
      #Comments capture:
369
      if ($read_line =~ /^--.*/) {
370
         $usm_comment[$prog_line_no] .= $read_line;
371
         next;
372
      }
373
      #Label capture:
374
      if ($read_line =~ s/^(.*):\s*//) {
375
         $label = $1;
376
         if ( exists $usm_label{$label} ) {
377
            print "Line $usmc_line_no: Error: Label '$label' has already been defined\n";
378
            $usmc_error_no++;
379
         }
380
         $usm_label{$label} = $prog_line_no;
381
         print "LL $prog_line_no, $label\n" if ($debug);
382
      }
383
      split( /\s+/, $read_line );
384
      $usm_command[$prog_line_no] = shift @_;
385
      $usm_data[$prog_line_no] = "@_";
386
      print "CM $usm_command[$prog_line_no]\n" if ($debug);
387
      print "DT $usm_data[$prog_line_no]\n" if ($debug);
388
      $command = $usm_command[$prog_line_no];
389
      {
390
         if ( grep(/^$command$/, @usm_ucode) ) { next; }
391
         if ( exists $usm_jump_code{$command} ) { next; }
392
         print "Line $usmc_line_no: Error: '$command' is not a valid opcode\n";
393
         $usmc_error_no++;
394
      }
395
      $prog_line_no++;
396
   }
397
   $usmc_prog_line_no = $prog_line_no;
398
   $usm_label{"end"} = $usmc_prog_line_no;
399
   $usm_command[$usmc_prog_line_no] = "jump";
400
   $usm_data[$usmc_prog_line_no] = "end";
401
   &s_label_err;
402
   return $_command_key, $read_line;
403
}
404
 
405
# Search for label errors:
406
sub s_label_err {
407
   foreach $index (0 .. $#usm_command ) {
408
      $command = $usm_command[$index];
409
      if ( $command =~ /^jump$/  ||
410
               exists $usm_jump_code{$command} ) {
411
         $label = $usm_data[$index];
412
         if ( !exists $usm_label{$label} ) {
413
            print "Error: '$label' is not a valid label in\n   '$command $label' (prog_code: $index)\n";
414
            $usmc_error_no++;
415
         }
416
      }
417
   }
418
}
419
 
420
# Reads the extra VHDL code:
421
sub rd_extra_code {
422
   while (1) {
423
      if ( &line_first_word(0) ) {
424
         return $_command_key, $read_line;
425
      }
426
      push(@usm_extra_code, $read_line);
427
   }
428
}
429
 
430
############################### Write subroutines #################################
431
 
432
# Writes a number of indentation segments: 
433
sub indent {
434
   my($i);
435
   for ($i = 0; $i < $_[0]; ++$i) {
436
      print OFILE "$_indent";
437
   }
438
}
439
 
440
# Writes entity header: 
441
sub wr_entity {
442
   print OFILE "$_line_end"."entity $usm_entity is$_line_end";
443
   if ( @usm_generic ) {
444
      &indent(1); print OFILE "generic ($_line_end";
445
      foreach $line ( @usm_generic ) { &indent(2); print OFILE $line; }
446
      &indent(1); print OFILE ");$_line_end";
447
   }
448
   &indent(1); print OFILE "port ($_line_end";
449
   &indent(2); print OFILE "-- To start the program:$_line_end";
450
   &indent(2); print OFILE "avuc_start: in std_logic;$_line_end";
451
   &indent(2); print OFILE "-- To stop the program:$_line_end";
452
   &indent(2); print OFILE "avuc_rst: in std_logic;$_line_end";
453
   foreach $line ( @usm_port ) { &indent(2); print OFILE $line; }
454
   &indent(2); print OFILE "-- State of the program (running/stopped):$_line_end";
455
   &indent(2); print OFILE "avuc_state: out std_logic$_line_end";
456
   &indent(1); print OFILE ");$_line_end"."end $usm_entity;$_line_end";
457
}
458
 
459
 
460
# Writes signal declaration of usm basic registers: 
461
sub wr_sig_decl_breg {
462
   print OFILE "-- Basic registers of usm:$_line_end";
463
   printf OFILE "signal usm_opcode: std_logic_vector(%u downto 0);$_line_end", $usm_opcode_bus_width-1;
464
   printf OFILE "signal usm_pc: std_logic_vector(%u downto 0) := (others => '1');$_line_end", $usm_pc_bus_width-1;
465
   printf OFILE "signal usm_data: std_logic_vector(%u downto 0);$_line_end", $usm_data_bus_width-1;
466
   printf OFILE "signal usm_cyclesno: std_logic_vector(%u downto 0);$_line_end", $usm_cycle_bus_width-1;
467
   print OFILE "-- Cycles counter:$_line_end";
468
   printf OFILE "signal usm_cy: std_logic_vector(%u downto 0);$_line_end", $usm_cycle_bus_width;
469
   print OFILE "-- usm_cy(0) delayed 1 clock:$_line_end";
470
   print OFILE "signal usm_cy_0_d1: std_logic;$_line_end";
471
   print OFILE "-- To avoid usm_cy deadlock:$_line_end";
472
   print OFILE "signal usm_cy_rst: std_logic;$_line_end$_line_end";
473
}
474
 
475
# Writes signal declaration of usm commands: 
476
sub wr_sig_decl_clist {
477
   my $ucode;
478
   print OFILE "-- Commands list:$_line_end";
479
   foreach $index ( 0 .. $#usm_ucode ) {
480
      $ucode = $usm_ucode[$index];
481
      print OFILE "constant USMO_" . uc($ucode) . ":$_line_end";
482
      &indent(3); printf OFILE "std_logic_vector(usm_opcode'range) := \"$usm_opcode_format\";$_line_end", $index;
483
   }
484
   $index = 1;
485
   foreach $jump ( keys %usm_jump_code ) {
486
#       $ucode = $usm_jump_code{$jump};
487
      print OFILE "constant USMO_" . uc($jump) . ":$_line_end";
488
      &indent(3); printf OFILE "std_logic_vector(usm_opcode'range) := \"$usm_opcode_format\";$_line_end", $#usm_ucode + $index;
489
      ++$index;
490
   }
491
   print OFILE $_line_end;
492
}
493
 
494
# Writes signal declaration of usm labels: 
495
sub wr_sig_decl_llist {
496
   my $label;
497
   print OFILE "-- Labels list:$_line_end";
498
   foreach $index ( keys %usm_label ) {
499
      $label = $index;
500
      printf OFILE "constant USML_%s:$_line_end", uc($label);
501
      &indent(3); printf OFILE "std_logic_vector(usm_pc'range) := \"$usm_pc_format\";$_line_end", $usm_label{$index};
502
   }
503
   print OFILE $_line_end;
504
}
505
 
506
# Writes signal declaration of usm ucodes: 
507
sub wr_sig_decl_ulist {
508
   my $ucode;
509
   print OFILE "-- usm_pc reset:$_line_end";
510
   print OFILE "signal avuc_start_d: std_logic_vector(2 downto 0);$_line_end";
511
   print OFILE "signal avuc_rst_d: std_logic_vector(2 downto 0);$_line_end";
512
   print OFILE "signal stup_rst, stup_rst_d1: std_logic;$_line_end";
513
   print OFILE "signal stup_rst_cnt: std_logic_vector(5 downto 0) := (others => '0');$_line_end";
514
   print OFILE "signal usm_pc_gt_end: std_logic;$_line_end";
515
   print OFILE "signal usm_pc_ldini_init_d1: std_logic;$_line_end";
516
   print OFILE "signal usm_pc_ldend_init_d1: std_logic;$_line_end";
517
   print OFILE "-- usm_pc microcodes:$_line_end";
518
   print OFILE "signal usm_pc_inc: std_logic;$_line_end";
519
   print OFILE "signal usm_pc_ldjmp: std_logic;$_line_end";
520
   print OFILE "signal usm_pc_ldini: std_logic;$_line_end";
521
   print OFILE "signal usm_pc_ldend: std_logic;$_line_end";
522
   print OFILE "-- Remaining microcodes:$_line_end";
523
#    foreach $index ( 0 .. $#usm_ucode ) {
524
#       $ucode = $usm_ucode[$index];
525
#       if ( grep(/^$ucode$/, @predefined_ucode) ) { next; }
526
#       printf OFILE "signal usm_%s: std_logic;$_line_end", $ucode;
527
#    }
528
   foreach $driver ( keys %usm_opcode ) {
529
      foreach $ucode ( keys %{ $usm_opcode{$driver} } ) {
530
         if ( $ucode !~ /^default$/ ) {
531
            foreach $cycle_no ( keys % { $usm_opcode{$driver}->{$ucode} } ) {
532
               print OFILE "signal usm_$ucode"."_$cycle_no".": std_logic;$_line_end";
533
            }
534
         }
535
      }
536
   }
537
   print OFILE $_line_end;
538
}
539
 
540
# Writes initial process: 
541
sub wr_proc_init {
542
   print OFILE "-- Program start/stop/state:$_line_end";
543
   print OFILE "p_prog_init: process($usm_main_clk)$_line_end";
544
   print OFILE "variable usm_pc_ld_init_var: std_logic;$_line_end";
545
   print OFILE "begin$_line_end";
546
   &indent(1); print OFILE "if rising_edge($usm_main_clk) then$_line_end";
547
   &indent(2); print OFILE "-- Program start:$_line_end";
548
   &indent(2); print OFILE "avuc_start_d(0) <= avuc_start;$_line_end";
549
   &indent(2); print OFILE "avuc_start_d(1) <= avuc_start_d(0);$_line_end";
550
   &indent(2); print OFILE "avuc_start_d(2) <= avuc_start_d(1);$_line_end";
551
   &indent(2); print OFILE "usm_pc_ld_init_var := avuc_start_d(1) and not avuc_start_d(2);$_line_end";
552
   &indent(2); print OFILE "usm_pc_ldini_init_d1 <= usm_pc_ld_init_var;$_line_end";
553
   &indent(2); print OFILE "usm_pc_ldini <= usm_pc_ld_init_var or usm_pc_ldini_init_d1;          -- 2 clocks long$_line_end";
554
   &indent(2); print OFILE "-- Program stop:$_line_end";
555
   &indent(2); print OFILE "if stup_rst = '1' then$_line_end";
556
   &indent(3); print OFILE "stup_rst_cnt <= stup_rst_cnt + 1;$_line_end";
557
   &indent(2); print OFILE "end if;$_line_end";
558
   &indent(2); print OFILE "stup_rst <= '0';$_line_end";
559
   &indent(2); print OFILE "if stup_rst_cnt(5 downto 4) /= 2 then$_line_end";
560
   &indent(3); print OFILE "stup_rst <= '1';$_line_end";
561
   &indent(2); print OFILE "end if;$_line_end";
562
   &indent(2); print OFILE "stup_rst_d1 <= stup_rst;$_line_end";
563
   &indent(2); print OFILE "usm_pc_gt_end <= '0';$_line_end";
564
   &indent(2); print OFILE "if usm_pc > USML_END and usm_pc_gt_end = '0' then$_line_end";
565
   &indent(3); print OFILE "usm_pc_gt_end <= '1';$_line_end";
566
   &indent(2); print OFILE "end if;$_line_end";
567
   &indent(2); print OFILE "avuc_rst_d(0) <= avuc_rst;$_line_end";
568
   &indent(2); print OFILE "avuc_rst_d(1) <= avuc_rst_d(0);$_line_end";
569
   &indent(2); print OFILE "avuc_rst_d(2) <= avuc_rst_d(1);$_line_end";
570
   &indent(2); print OFILE "usm_pc_ld_init_var := usm_pc_gt_end or (not stup_rst and stup_rst_d1) or$_line_end";
571
   &indent(15); print OFILE "( avuc_rst_d(1) and not avuc_rst_d(2) );$_line_end";
572
   &indent(2); print OFILE "usm_pc_ldend_init_d1 <= usm_pc_ld_init_var;$_line_end";
573
   &indent(2); print OFILE "usm_pc_ldend <= usm_pc_ld_init_var or usm_pc_ldend_init_d1;          -- 2 clocks long$_line_end";
574
   &indent(2); print OFILE "-- Program state:$_line_end";
575
   &indent(2); print OFILE "avuc_state <= AVUC_STATE_RUNNING;$_line_end";
576
   &indent(2); print OFILE "if usm_pc = USML_END then$_line_end";
577
   &indent(3); print OFILE "avuc_state <= AVUC_STATE_STOPPED;$_line_end";
578
   &indent(2); print OFILE "end if;$_line_end";
579
   &indent(1); print OFILE "end if;$_line_end";
580
   print OFILE "end process p_prog_init;$_line_end$_line_end";
581
}
582
 
583
# Writes program: 
584
sub wr_program {
585
   my $comment, $ucode_prog, $cycle_no_max, $data;
586
   print OFILE "p_usm_prog_code: process(usm_pc)$_line_end";
587
   print OFILE "begin$_line_end";
588
   &indent(1); print OFILE "usm_data <= (others => '-');$_line_end";
589
   &indent(1); print OFILE "case usm_pc is$_line_end";
590
   foreach $prog_line ( 0 .. $usmc_prog_line_no ) {
591
      &indent(1); printf OFILE "when \"$usm_pc_format\" =>$_line_end", $prog_line;
592
      # Comments:
593
      $comment = $usm_comment[$prog_line];
594
      if ( $comment ) {
595
         &indent(2); print OFILE "$comment";
596
      }
597
      # usm_opcode:
598
      $ucode_prog = $usm_command[$prog_line];
599
      &indent(2); print OFILE "usm_opcode <= USMO_".uc($ucode_prog).";$_line_end";
600
      # usm_cyclesno:
601
      $cycle_no_max = 0;
602
      if ( !grep(/^$ucode_prog/, @predefined_ucode) && !exists $usm_jump_code{$ucode_prog} ) {
603
         outer_loop: foreach $driver (keys %usm_opcode) {
604
            foreach $ucode ( keys %{ $usm_opcode{$driver} } ) {
605
               if ( $ucode =~ /^$ucode_prog$/ ) {
606
                  foreach $cycle_no ( keys %{$usm_opcode{$driver}->{$ucode} } ) {
607
                     if ( $cycle_no > cycle_no_max ) { $cycle_no_max = $cycle_no; }
608
                  }
609
                  last outer_loop;
610
               }
611
            }
612
         }
613
      }
614
      &indent(2); printf OFILE "usm_cyclesno <= (%u => '1', others => '0');$_line_end", $cycle_no_max;
615
      # usm_data:
616
      $data = $usm_data[$prog_line];
617
      if ( !$data =~ /\D/ ) {
618
         &indent(2); printf OFILE "usm_data <= \"$usm_data_format\";$_line_end", $data;
619
      } elsif ( exists $usm_label{$data}  ) {
620
         &indent(2); printf OFILE "usm_data <= \"$usm_data_format\";$_line_end", $usm_label{$data};
621
      } elsif ( $data ) {
622
         &indent(2); printf OFILE "usm_data(%u downto 0) <= %s;$_line_end", $usm_data_bus_width-1, $data;
623
      }
624
   }
625
   &indent(1); print OFILE "when others =>$_line_end";
626
   &indent(2); print OFILE "usm_opcode <= (others => '-');$_line_end";
627
   &indent(2); print OFILE "usm_cyclesno <= (others => '-');$_line_end";
628
   &indent(1); print OFILE "end case;$_line_end";
629
   print OFILE "end process p_usm_prog_code;$_line_end$_line_end";
630
}
631
 
632
# Writes jump processes: 
633
sub wr_jump {
634
   # pc counter process:
635
   print OFILE "p_usm_pc_drv: process($usm_main_clk)$_line_end";
636
   print OFILE "variable var_aux: std_logic_vector(3 downto 0);$_line_end";
637
   print OFILE "begin$_line_end";
638
   &indent(1); print OFILE "if rising_edge($usm_main_clk) then$_line_end";
639
   &indent(2); print OFILE "-- usm_pc_inc (increment usm_pc):$_line_end";
640
   &indent(2); print OFILE "usm_pc_inc <= '0';$_line_end";
641
   &indent(2); print OFILE "if usm_cy = usm_cyclesno and usm_pc_ldini_init_d1 = '0' then$_line_end";
642
   &indent(3); print OFILE "usm_pc_inc <= '1';$_line_end";
643
   &indent(2); print OFILE "end if;$_line_end";
644
   &indent(2); print OFILE "-- usm_pc_ldjmp (load usm_pc with usm_data):$_line_end";
645
   &indent(2); print OFILE "usm_pc_ldjmp <= '0';$_line_end";
646
   &indent(2); print OFILE "case usm_opcode is$_line_end";
647
   &indent(2); print OFILE "when USMO_JUMP =>$_line_end";
648
   &indent(3); print OFILE "usm_pc_ldjmp <= '1';$_line_end";
649
   foreach $jump ( keys %usm_jump_code ) {
650
      &indent(2); print OFILE "when USMO_".uc($jump)." =>$_line_end";
651
      &indent(3); print OFILE "if $usm_jump_code{$jump}then$_line_end", ;
652
      &indent(4); print OFILE "usm_pc_ldjmp <= '1';$_line_end";
653
      &indent(3); print OFILE "end if;$_line_end";
654
   }
655
   &indent(2); print OFILE "when others =>$_line_end";
656
   &indent(3); print OFILE "usm_pc_ldjmp <= '0';$_line_end";
657
   &indent(2); print OFILE "end case;$_line_end";
658
   &indent(2); print OFILE "-- usm_pc:$_line_end";
659
   &indent(2); print OFILE "usm_cy_0_d1 <= usm_cy(0) and not usm_pc_ldini_init_d1;$_line_end";
660
   &indent(2); print OFILE "var_aux := usm_pc_ldini & usm_pc_ldend & (usm_pc_ldjmp and usm_cy_0_d1) & usm_pc_inc;$_line_end";
661
   &indent(2); print OFILE "case var_aux is$_line_end";
662
   &indent(2); print OFILE "when \"0000\" =>$_line_end";
663
   &indent(3); print OFILE "null;$_line_end";
664
   &indent(2); print OFILE "when \"0001\" =>$_line_end";
665
   &indent(3); print OFILE "usm_pc <= usm_pc + 1;$_line_end";
666
   &indent(2); print OFILE "when \"0010\" | \"0011\" =>$_line_end";
667
   &indent(3); print OFILE "usm_pc <= usm_data(usm_pc'range);$_line_end";
668
   &indent(2); print OFILE "when \"0100\" | \"0101\" | \"0110\" | \"0111\" =>$_line_end";
669
   &indent(3); print OFILE "usm_pc <= USML_END;$_line_end";
670
   &indent(2); print OFILE "when \"1000\" | \"1001\" | \"1010\" | \"1011\" | \"1100\" | \"1101\" | \"1110\" | \"1111\" =>       -- 1XXX$_line_end";
671
   &indent(3); print OFILE "usm_pc <= USML_BEGIN;$_line_end";
672
   &indent(2); print OFILE "when others =>$_line_end";
673
   &indent(4); print OFILE "null;$_line_end";
674
   print OFILE "--          usm_pc <= USML_BEGIN;$_line_end";
675
   &indent(2); print OFILE "end case;$_line_end";
676
   &indent(1); print OFILE "end if;$_line_end";
677
   print OFILE "end process p_usm_pc_drv;$_line_end$_line_end";
678
   # Cycle process:
679
   printf OFILE "p_usm_cy_drv: process(%s)$_line_end", $usm_main_clk;
680
   print OFILE "begin$_line_end";
681
   &indent(1); printf OFILE "if rising_edge(%s) then$_line_end", $usm_main_clk;
682
   &indent(2); print OFILE "usm_cy_rst <= '0';$_line_end";
683
   &indent(2); print OFILE "if usm_cy = 0 then$_line_end";
684
   &indent(3); print OFILE "usm_cy_rst <= '1';$_line_end";
685
   &indent(2); print OFILE "end if;$_line_end";
686
   &indent(2); print OFILE "-- usm_cy:$_line_end";
687
   &indent(2); print OFILE "if usm_pc_inc = '1' or (usm_pc_ldjmp = '1' and usm_cy_0_d1 = '1') or$_line_end";
688
   &indent(12); print OFILE "usm_pc_ldini_init_d1 = '1' or usm_cy_rst = '1' then$_line_end";
689
   &indent(3); print OFILE "usm_cy <= (0 => '1', others => '0');$_line_end";
690
   &indent(2); print OFILE "else$_line_end";
691
   &indent(3); print OFILE "usm_cy(0) <= '0';$_line_end";
692
   &indent(3); print OFILE "for i in usm_cy'range loop$_line_end";
693
   &indent(4); print OFILE "if i /= 0 then$_line_end";
694
   &indent(5); print OFILE "usm_cy(i) <= usm_cy(i-1);$_line_end";
695
   &indent(4); print OFILE "end if;$_line_end";
696
   &indent(3); print OFILE "end loop;$_line_end";
697
   &indent(2); print OFILE "end if;$_line_end";
698
   &indent(1); print OFILE "end if;$_line_end";
699
   print OFILE "end process p_usm_cy_drv;$_line_end$_line_end";
700
}
701
 
702
# Writes drivers: 
703
sub wr_drivers {
704
   my %ucode_hash_uc, %ucode_hash_act, %ucode_one_hot, $index_ucode;
705
   foreach $driver ( keys %usm_opcode ) {
706
 
707
      # Auxiliary hashes:
708
      %ucode_hash_uc = %{ $usm_opcode{$driver} };
709
      %ucode_hash_act = ();
710
      foreach $ucode ( keys %{ $usm_opcode{$driver} } ) {
711
         if ( $ucode !~ /^default$/ ) {
712
            foreach $cycle_no ( keys %{ $usm_opcode{$driver}->{$ucode} } ) {
713
               $ucode_hash_act{"$ucode"."_"."$cycle_no"} = $usm_opcode{$driver}->{$ucode}->{$cycle_no} ;
714
            }
715
         }
716
      }
717
 
718
      # Process beginning:
719
      print OFILE "$_line_end";
720
      print OFILE "p_usm_$driver"."_drv: process($usm_main_clk)$_line_end";
721
      $var_data_bus_width = scalar(keys %ucode_hash_act);
722
      $var_data_format = "%0".$var_data_bus_width."b";
723
      printf OFILE "variable %s_var: std_logic_vector(%u downto 0);$_line_end", $driver, $var_data_bus_width-1;
724
      print OFILE "begin$_line_end";
725
      &indent(1); print OFILE "if rising_edge($usm_main_clk) then$_line_end";
726
 
727
      # Microcodes:
728
      foreach $ucode ( keys %ucode_hash_uc ) {
729
         if ( $ucode !~ /^default$/ ) {
730
            &indent(2); print OFILE "-- $ucode:$_line_end";
731
            foreach $cycle_no ( keys %{ $ucode_hash_uc{$ucode} } ) {
732
               &indent(2); printf OFILE "usm_$ucode"."_$cycle_no <= '0';$_line_end";
733
            }
734
            &indent(2); print OFILE "if usm_opcode = USMO_".uc($ucode)." then$_line_end";
735
            foreach $cycle_no ( keys %{ $ucode_hash_uc{$ucode} } ) {
736
               &indent(3); printf OFILE "if usm_cy(%u) = '1' then$_line_end", $cycle_no;
737
               &indent(4); print OFILE "usm_$ucode"."_$cycle_no <= '1';$_line_end";
738
               &indent(3); print OFILE "end if;$_line_end";
739
            }
740
            &indent(2); print OFILE "end if;$_line_end";
741
         }
742
      }
743
 
744
      # Actions:
745
      &indent(2); print OFILE "-- Actions:$_line_end";
746
      &indent(2); print OFILE "$driver"."_var := ";
747
      $index_ucode = 0;
748
      foreach $ucode ( keys %ucode_hash_act ) {
749
         if ( $index_ucode ) { print OFILE " & "; }
750
         $ucode_one_hot{$ucode} = "";
751
         foreach $index_one_hot ( 0 .. $var_data_bus_width-1) {
752
            $bit = 0;
753
            if ( $index_ucode == $index_one_hot ) { $bit = 1; }
754
            $ucode_one_hot{$ucode} .= $bit;
755
         }
756
         print OFILE "usm_$ucode";
757
         ++$index_ucode;
758
      }
759
      print OFILE ";$_line_end";
760
      &indent(2); print OFILE "case $driver"."_var is$_line_end";
761
      &indent(2); printf OFILE "when \"$var_data_format\" =>", 0;
762
      if ( defined($usm_opcode{$driver}->{default}) ) {
763
         print OFILE $_line_end;
764
         foreach $line ( @{ $usm_opcode{$driver}->{default} } ) {
765
            &indent(3); print OFILE $line;
766
         }
767
      } else {
768
         print OFILE " null;$_line_end";
769
      }
770
      foreach $ucode ( keys %ucode_hash_act ) {
771
         &indent(2); print OFILE "when \"$ucode_one_hot{$ucode}\" =>$_line_end";
772
         foreach $line ( @{ $ucode_hash_act{$ucode} } ) {
773
            &indent(3); print OFILE $line;
774
         }
775
      }
776
      &indent(2); print OFILE "when others => null;$_line_end";
777
      &indent(2); print OFILE "end case;$_line_end";
778
      # End process:
779
      &indent(1); print OFILE "end if;$_line_end";
780
      print OFILE "end process p_usm_"."$driver"."_drv;$_line_end$_line_end";
781
   }
782
}
783
 

powered by: WebSVN 2.1.0

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