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 10

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 10 feddischso
  "  > new <> <> <>   # opens a system-on-chip file
100 3 feddischso
         - <>     : the SOC name
101 10 feddischso
         - <>       : the SOC id
102 3 feddischso
         - <> : 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 10 feddischso
      @soc_inst = SOCMaker::CoreInst.new( "#{@soc.version.to_s}" )
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 10 feddischso
  "  > add <> <>
156
                        # adds an ip-core from the library to the SOC
157
        - <>      : id of the IP core
158
        - <>    : instanciation name
159 3 feddischso
 
160
  "
161
  def do_add( args )
162 10 feddischso
    if args.size != 2
163
      puts "two arguments are required:\nusage:\n#{ADD_USAGE}"
164 3 feddischso
    else
165 10 feddischso
      puts FMSG if @soc.add_core( args[ 0 ], args[ 1 ] ) == nil
166 3 feddischso
    end
167
  end
168
 
169
 
170
  #
171
  # Set/Get Parameter
172
  #
173
  PARAMETER_USAGE =
174
  "  > prameter <> <> <>
175
                        # modifies a parameter of an instance
176
       - <>   : the instance name of the core
177
       - <>  : the instance parameter name
178
       - <>      : the value which is set (optional). The current
179
                          value is printed, if omitted
180
  "
181
  def do_parameter( args )
182
    if args.size == 2
183
      puts FMSG if @soc.get_param( args[ 0 ], args[ 1 ] ) == nil
184
    elsif args.size == 3
185
      puts FMSG if @soc.set_param( args[ 0 ], args[ 1 ], args[ 2 ] ) == nil
186
    else
187
      puts "two or three arguments required:\nusage:\n#{PARAMETER_USAGE}"
188
    end
189
  end
190
 
191
 
192
  #
193
  # Set/Get Static Parameter
194
  #
195
  SPARAMETER_USAGE =
196
  "  > sprameter <> <> <>
197
                        # modifies the static parameter of a core
198
       - <>       : the name of the core
199
       - <>  : the static parameter name
200
       - <>      : the value which is set (optional). The current
201
                          value is printed, if omitted
202
  "
203
  def do_sparameter( args )
204
    if args.size == 2
205
      puts FMSG if @soc.get_sparam( args[ 0 ], args[ 1 ] ) == nil
206
    elsif args.size == 3
207
      puts FMSG if @soc.set_sparam( args[ 0 ], args[ 1 ], args[ 2 ] ) == nil
208
    else
209
      puts "two or three arguments required:\nusage:\n#{SPARAMETER_USAGE}"
210
    end
211
  end
212
 
213
 
214
  #
215
  # Connect
216
  #
217
  CONNECT_USAGE =
218
  "  > connect <> <> <> <> <>
219
                        # connects two cores
220
        - <>     : instance name of the first core
221
        - <>     : instance name of the second core
222
        - <>      : interface name of the first core
223
        - <>      : interface name of the second core
224
        - <>      : connection name
225
 
226
  "
227
  def do_connect( args )
228
    if args.size != 5
229
      puts "five arguments are required:\nusage:\n#{CONNECT_USAGE}"
230
    else
231
      puts FMSG if @soc.add_connection( args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ] )
232
    end
233
  end
234
 
235
 
236
  #
237
  # Delete
238
  #
239
  DELETE_USAGE =
240
  "  > delete <>
241
                        # removes a core or a connection
242
        - < : the core or connection, which is removed
243
 
244
  "
245
  def do_delete( args )
246
    if args.size != 1
247
      puts "five arguments are required:\nusage:\n#{DELETE_USAGE}"
248
    else
249
      puts FMSG if @soc.rm( args[ 0 ] ) == nil
250
    end
251
  end
252
 
253
 
254
  #
255
  # Save
256
  #
257
  SAVE_USAGE =
258
  "  > save <>    # saves system-on-chip definition in YAML format to file
259
        - <>     : optional destination file, when omitted: the
260
                         original file-path is used
261
 
262
  "
263
  def do_save( args )
264
    if args.size > 1
265
      puts "zero or one argument is required:\nusage:\n#{SAVE_USAGE}"
266
    else
267
      p args
268
      puts FMSG if @soc.save_yaml( args ) == nil
269
    end
270
  end
271
 
272
 
273
  #
274
  # Generate
275
  #
276
  GENERATE_USAGE =
277
  "  > generate         # generates a synthesizable system-on-chip implementation
278
 
279
  "
280
  def do_generate( args )
281
    if args.size != 0
282
      puts "no arguments are required:\nusage:\n#{GENERATE_USAGE}"
283
    else
284 6 feddischso
      @soc_inst.gen_toplevel
285 3 feddischso
      @soc.copy_files
286
    end
287
  end
288
 
289
 
290 5 feddischso
 
291
  PRINT_USAGE =
292
  "  > print            # prints SOC information
293
 
294
  "
295
  def do_print( args )
296
    if args.size != 0
297
      puts "no arguments are required:\nusage:\n#{PRINT_USAGE}"
298
    else
299
      puts @soc
300
    end
301
  end
302
 
303
 
304 3 feddischso
  #
305
  # Quit
306
  #
307
  QUIT_USAGE =
308
  "  > quit             # the same than exit
309
 
310
  "
311
  def do_quit( args )
312
    do_exit( args )
313
  end
314
 
315
 
316
  #
317
  # Exit
318
  #
319
  EXIT_USAGE =
320
  "  > exit             # exits this tool
321
 
322
  "
323
  def do_exit( args )
324
    puts "... bye bye!"
325
    exit 0
326
  end
327
 
328
 
329
  #
330
  # Help
331
  #
332
  HELP_USAGE =
333
  "  > help             # prints some help information
334
 
335
  "
336
  def do_help( args )
337
    puts "The following commands are available:\n\n"
338
    @commands.each { |c| eval "puts  #{c.upcase}_USAGE" }
339
  end
340
 
341 5 feddischso
 
342
  SET_USAGE =
343
  "  > set              # not implemented yet
344
 
345
  "
346
  def do_set( args )
347
    puts "NOT IMPLEMENTED, YET"
348
  end
349
 
350
  GET_USAGE =
351
  "  > get              # not implemented yet
352
 
353
  "
354
  def do_get( args )
355
    puts "NOT IMPLEMENTED, YET"
356
  end
357
 
358 3 feddischso
  #  end command implementations
359
  #
360
  #################################
361
 
362
 
363
 
364 6 feddischso
  @soc      = nil
365
  @soc_inst = nil
366 3 feddischso
 
367
  def initialize
368
 
369
    # appreviation map
370
    @appr_map = { 'n' => "new",
371
                  'o' => "open",
372
                  'q' => "quit",
373
                  'h' => "help",
374
                  'l' => "list",
375
                  'a' => "add",
376
                  'g' => "generate",
377
                  's' => "save",
378
                  'p' => "parameter",
379
                  'd' => "delete",
380
                  'c' => "connect",
381 5 feddischso
                  'i' => "print",
382 3 feddischso
                  'x' => "exit"
383
                  }
384
 
385
    # all available commands
386
    @commands = %w[ new open list add parameter sparameter
387
                    delete connect save help quit exit
388 5 feddischso
                    generate print set get ]
389 3 feddischso
 
390 5 feddischso
    comp = proc { |s| (@commands + Dir.entries( Dir.pwd )).grep( /^#{Regexp.escape(s)}/ ) }
391 3 feddischso
    Readline.completion_append_character = " "
392
    Readline.completion_proc = comp
393
 
394
  end
395
 
396
  def run
397
 
398
    ##
399
    # process user commands
400
    #
401
    while buf = Readline.readline( "> ", true )
402
      process_cmd buf
403
    end
404
  end
405
 
406
  def process_cmd( c )
407
 
408
      # remove the comments and split each line
409
      match = SOCMaker::conf[ :COMMENT_REGEX ].match( c )
410
      cmd_arr = match[1].split( ' ' )
411
 
412
      # process the command, if there is one
413
      if cmd_arr.size > 0
414
        cmd     = ""
415
        if cmd_arr[ 0 ].size == 1 and @appr_map[ cmd_arr[ 0 ] ] != nil
416
          cmd = @appr_map[ cmd_arr[ 0 ] ]
417
        else
418
          cmd = cmd_arr[ 0 ]
419
        end
420
 
421
        if @commands.include?( cmd )
422
          cmd_str = "do_#{cmd}( cmd_arr[ 1..-1] )"
423
          puts "evaluating >>#{cmd_str}<< "
424
          eval( cmd_str )
425
        #TODO this is for linux only
426
        elsif system( "which #{cmd} > /dev/null 2>&1" )
427
          system( c )
428
        else
429
          puts "Command #{cmd} not available"
430
        end
431
       #begin
432
       #rescue
433
       #  puts "evaluating >>#{cmd_str}<< failed"
434
       #end
435
      end
436
  end
437
 
438
  @@inst = nil
439
 
440
 
441
end
442
end
443
 
444
 
445
# vim: noai:ts=2:sw=2
446
 

powered by: WebSVN 2.1.0

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