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

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [cli.rb] - Blame information for rev 6

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

Line No. Rev Author Line
1 3 feddischso
#!/usr/bin/env ruby
2
###############################################################
3
#
4
#  File:      soc_maker_cli.rb
5
#
6
#  Author:    Christian Hättich
7
#
8
#  Project:   System-On-Chip Maker
9
#
10
#  Target:    Linux / Windows / Mac
11
#
12
#  Language:  ruby
13
#
14
#
15
###############################################################
16
#
17
#
18
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
19
#
20
#   This program is free software: you can redistribute it and/or modify
21
#   it under the terms of the GNU General Public License as published by
22
#   the Free Software Foundation, either version 3 of the License, or
23
#   (at your option) any later version.
24
#
25
#   This program is distributed in the hope that it will be useful,
26
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
#   GNU General Public License for more details.
29
#
30
#   You should have received a copy of the GNU General Public License
31
#   along with this program.  If not, see .
32
#
33
#
34
###############################################################
35
#
36
#   Description:
37
#
38
#     Command-line interface for accessing the SOC-Maker functionallity
39
#
40
#     The following commands are available:
41
#       - new         -> create a new soc file
42
#       - open        -> open soc file
43
#       - list        -> list library
44
#       - add         -> add core to soc
45
#       - parameter   -> set/get parameter
46
#       - sparameter  -> set/get static parameter
47
#       - connect     -> connect cores
48
#       - delete      -> delete core or connection
49
#       - save        -> save soc
50
#       - generate    -> generate soc
51
#       - quit        -> quit this CLI
52
#       - exit        -> same than quit
53
#       - help        -> print some help
54
#
55
#     Please use the help command to get more information about
56
#     each command and its parameters.
57
#
58
#     This CLI is a wrapper around SOCMaker::SOCDef.
59
#
60
#
61
#######
62
#
63
# TODO: add commands for
64
#       - selecting the coder
65
#         (at the moment, only VHDL is supported)
66
#
67
#       - refreshing the lib
68
#
69
#
70
###############################################################
71
require 'readline'
72
require 'optparse'
73
 
74
 
75
module SOCMaker
76
class Cli
77
 
78
  private_class_method :new
79
  def Cli.instance
80
      @@inst = new if @@inst == nil
81
      return @@inst
82
  end
83
 
84
  FMSG      = ' -> failed '
85
 
86
 
87
 
88
  #########################################
89
  #
90
  # command implementations:
91
  #   -> a usage string and
92
  #      a function for every command
93
  #
94
 
95
  #
96
  # New
97
  #
98
  NEW_USAGE =
99
  "  > new <> <> <>   # opens a system-on-chip file
100
         - <>     : the SOC name
101
         - <>  : the SOC version
102
         - <> : the toplevel name
103
  "
104
  def do_new( args )
105
    if args.size != 3
106
      puts "three arguments are required:\nusage:\n#{NEW_USAGE}"
107
    else
108
      @soc = SOCMaker::SOCDef.new( args[0], args[1], args[2] )
109 6 feddischso
      SOCMaker::lib.add_core( @soc )
110
      @soc_inst = SOCMaker::CoreInst.new( "#{args[0]}#{args[1]}" )
111 3 feddischso
      #puts FMSG if @soc.load_soc( args[ 0 ] ) == nil
112
    end
113
  end
114
 
115
 
116
 
117
  #
118
  # Open
119
  #
120
  OPEN_USAGE =
121
  "  > open <>    # opens a system-on-chip file
122
         - <>    : system-on-chip definition in in YAML format
123
 
124
  "
125
  def do_open( args )
126
    if args.size != 1
127
      puts "only one argument is required:\nusage:\n#{OPEN_USAGE}"
128
    else
129
      puts "loading #{args[0]}"
130
      @soc = SOCMaker::from_f( args[0] )
131 6 feddischso
      SOCMaker::lib.add_core( @soc )
132
      @soc_inst = SOCMaker::CoreInst.new( "#{@soc.name}#{@soc.version}" )
133 3 feddischso
      #puts FMSG if @soc.load_soc( args[ 0 ] ) == nil
134
    end
135
  end
136
 
137
 
138
  #
139
  # List
140
  #
141
  LIST_USAGE =
142
  "  > list             # prints list of cores and interfaces,
143
                          which are in the library
144
 
145
  "
146
  def do_list( args )
147
    puts SOCMaker::lib
148
  end
149
 
150
 
151
  #
152
  # Add
153
  #
154
  ADD_USAGE =
155
  "  > add <> <> <>
156
                        # adds a ip-core from the library to the SOC
157
        - <>      : name of the IP core
158
        - <>   : version of the IP core
159
        - <>      : instanciation name
160
 
161
  "
162
  def do_add( args )
163
    if args.size != 3
164
      puts "three arguments are required:\nusage:\n#{ADD_USAGE}"
165
    else
166
      puts FMSG if @soc.add_core( args[ 0 ], args[ 1 ], args[ 2 ] ) == nil
167
    end
168
  end
169
 
170
 
171
  #
172
  # Set/Get Parameter
173
  #
174
  PARAMETER_USAGE =
175
  "  > prameter <> <> <>
176
                        # modifies a parameter of an instance
177
       - <>   : the instance name of the core
178
       - <>  : the instance parameter name
179
       - <>      : the value which is set (optional). The current
180
                          value is printed, if omitted
181
  "
182
  def do_parameter( args )
183
    if args.size == 2
184
      puts FMSG if @soc.get_param( args[ 0 ], args[ 1 ] ) == nil
185
    elsif args.size == 3
186
      puts FMSG if @soc.set_param( args[ 0 ], args[ 1 ], args[ 2 ] ) == nil
187
    else
188
      puts "two or three arguments required:\nusage:\n#{PARAMETER_USAGE}"
189
    end
190
  end
191
 
192
 
193
  #
194
  # Set/Get Static Parameter
195
  #
196
  SPARAMETER_USAGE =
197
  "  > sprameter <> <> <>
198
                        # modifies the static parameter of a core
199
       - <>       : the name of the core
200
       - <>  : the static parameter name
201
       - <>      : the value which is set (optional). The current
202
                          value is printed, if omitted
203
  "
204
  def do_sparameter( args )
205
    if args.size == 2
206
      puts FMSG if @soc.get_sparam( args[ 0 ], args[ 1 ] ) == nil
207
    elsif args.size == 3
208
      puts FMSG if @soc.set_sparam( args[ 0 ], args[ 1 ], args[ 2 ] ) == nil
209
    else
210
      puts "two or three arguments required:\nusage:\n#{SPARAMETER_USAGE}"
211
    end
212
  end
213
 
214
 
215
  #
216
  # Connect
217
  #
218
  CONNECT_USAGE =
219
  "  > connect <> <> <> <> <>
220
                        # connects two cores
221
        - <>     : instance name of the first core
222
        - <>     : instance name of the second core
223
        - <>      : interface name of the first core
224
        - <>      : interface name of the second core
225
        - <>      : connection name
226
 
227
  "
228
  def do_connect( args )
229
    if args.size != 5
230
      puts "five arguments are required:\nusage:\n#{CONNECT_USAGE}"
231
    else
232
      puts FMSG if @soc.add_connection( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ] )
233
    end
234
  end
235
 
236
 
237
  #
238
  # Delete
239
  #
240
  DELETE_USAGE =
241
  "  > delete <>
242
                        # removes a core or a connection
243
        - < : the core or connection, which is removed
244
 
245
  "
246
  def do_delete( args )
247
    if args.size != 1
248
      puts "five arguments are required:\nusage:\n#{DELETE_USAGE}"
249
    else
250
      puts FMSG if @soc.rm( args[ 0 ] ) == nil
251
    end
252
  end
253
 
254
 
255
  #
256
  # Save
257
  #
258
  SAVE_USAGE =
259
  "  > save <>    # saves system-on-chip definition in YAML format to file
260
        - <>     : optional destination file, when omitted: the
261
                         original file-path is used
262
 
263
  "
264
  def do_save( args )
265
    if args.size > 1
266
      puts "zero or one argument is required:\nusage:\n#{SAVE_USAGE}"
267
    else
268
      p args
269
      puts FMSG if @soc.save_yaml( args ) == nil
270
    end
271
  end
272
 
273
 
274
  #
275
  # Generate
276
  #
277
  GENERATE_USAGE =
278
  "  > generate         # generates a synthesizable system-on-chip implementation
279
 
280
  "
281
  def do_generate( args )
282
    if args.size != 0
283
      puts "no arguments are required:\nusage:\n#{GENERATE_USAGE}"
284
    else
285 6 feddischso
      @soc_inst.gen_toplevel
286 3 feddischso
      @soc.copy_files
287
    end
288
  end
289
 
290
 
291 5 feddischso
 
292
  PRINT_USAGE =
293
  "  > print            # prints SOC information
294
 
295
  "
296
  def do_print( args )
297
    if args.size != 0
298
      puts "no arguments are required:\nusage:\n#{PRINT_USAGE}"
299
    else
300
      puts @soc
301
    end
302
  end
303
 
304
 
305 3 feddischso
  #
306
  # Quit
307
  #
308
  QUIT_USAGE =
309
  "  > quit             # the same than exit
310
 
311
  "
312
  def do_quit( args )
313
    do_exit( args )
314
  end
315
 
316
 
317
  #
318
  # Exit
319
  #
320
  EXIT_USAGE =
321
  "  > exit             # exits this tool
322
 
323
  "
324
  def do_exit( args )
325
    puts "... bye bye!"
326
    exit 0
327
  end
328
 
329
 
330
  #
331
  # Help
332
  #
333
  HELP_USAGE =
334
  "  > help             # prints some help information
335
 
336
  "
337
  def do_help( args )
338
    puts "The following commands are available:\n\n"
339
    @commands.each { |c| eval "puts  #{c.upcase}_USAGE" }
340
  end
341
 
342 5 feddischso
 
343
  SET_USAGE =
344
  "  > set              # not implemented yet
345
 
346
  "
347
  def do_set( args )
348
    puts "NOT IMPLEMENTED, YET"
349
  end
350
 
351
  GET_USAGE =
352
  "  > get              # not implemented yet
353
 
354
  "
355
  def do_get( args )
356
    puts "NOT IMPLEMENTED, YET"
357
  end
358
 
359 3 feddischso
  #  end command implementations
360
  #
361
  #################################
362
 
363
 
364
 
365 6 feddischso
  @soc      = nil
366
  @soc_inst = nil
367 3 feddischso
 
368
  def initialize
369
 
370
    # appreviation map
371
    @appr_map = { 'n' => "new",
372
                  'o' => "open",
373
                  'q' => "quit",
374
                  'h' => "help",
375
                  'l' => "list",
376
                  'a' => "add",
377
                  'g' => "generate",
378
                  's' => "save",
379
                  'p' => "parameter",
380
                  'd' => "delete",
381
                  'c' => "connect",
382 5 feddischso
                  'i' => "print",
383 3 feddischso
                  'x' => "exit"
384
                  }
385
 
386
    # all available commands
387
    @commands = %w[ new open list add parameter sparameter
388
                    delete connect save help quit exit
389 5 feddischso
                    generate print set get ]
390 3 feddischso
 
391 5 feddischso
    comp = proc { |s| (@commands + Dir.entries( Dir.pwd )).grep( /^#{Regexp.escape(s)}/ ) }
392 3 feddischso
    Readline.completion_append_character = " "
393
    Readline.completion_proc = comp
394
 
395
  end
396
 
397
  def run
398
 
399
    ##
400
    # process user commands
401
    #
402
    while buf = Readline.readline( "> ", true )
403
      process_cmd buf
404
    end
405
  end
406
 
407
  def process_cmd( c )
408
 
409
      # remove the comments and split each line
410
      match = SOCMaker::conf[ :COMMENT_REGEX ].match( c )
411
      cmd_arr = match[1].split( ' ' )
412
 
413
      # process the command, if there is one
414
      if cmd_arr.size > 0
415
        cmd     = ""
416
        if cmd_arr[ 0 ].size == 1 and @appr_map[ cmd_arr[ 0 ] ] != nil
417
          cmd = @appr_map[ cmd_arr[ 0 ] ]
418
        else
419
          cmd = cmd_arr[ 0 ]
420
        end
421
 
422
        if @commands.include?( cmd )
423
          cmd_str = "do_#{cmd}( cmd_arr[ 1..-1] )"
424
          puts "evaluating >>#{cmd_str}<< "
425
          eval( cmd_str )
426
        #TODO this is for linux only
427
        elsif system( "which #{cmd} > /dev/null 2>&1" )
428
          system( c )
429
        else
430
          puts "Command #{cmd} not available"
431
        end
432
       #begin
433
       #rescue
434
       #  puts "evaluating >>#{cmd_str}<< failed"
435
       #end
436
      end
437
  end
438
 
439
  @@inst = nil
440
 
441
 
442
end
443
end
444
 
445
 
446
# vim: noai:ts=2:sw=2
447
 

powered by: WebSVN 2.1.0

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