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 5

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

powered by: WebSVN 2.1.0

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