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 3

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
  #
289
  # Quit
290
  #
291
  QUIT_USAGE =
292
  "  > quit             # the same than exit
293
 
294
  "
295
  def do_quit( args )
296
    do_exit( args )
297
  end
298
 
299
 
300
  #
301
  # Exit
302
  #
303
  EXIT_USAGE =
304
  "  > exit             # exits this tool
305
 
306
  "
307
  def do_exit( args )
308
    puts "... bye bye!"
309
    exit 0
310
  end
311
 
312
 
313
  #
314
  # Help
315
  #
316
  HELP_USAGE =
317
  "  > help             # prints some help information
318
 
319
  "
320
  def do_help( args )
321
    puts "The following commands are available:\n\n"
322
    @commands.each { |c| eval "puts  #{c.upcase}_USAGE" }
323
  end
324
 
325
  #  end command implementations
326
  #
327
  #################################
328
 
329
 
330
 
331
  @soc = nil
332
 
333
  def initialize
334
    ##
335
    # Setup readline
336
    #
337
    @cmd_list = %w[ list generate open exit
338
                   help quit add parameter save
339
                   connect sparameter delete ].sort
340
 
341
    # appreviation map
342
    @appr_map = { 'n' => "new",
343
                  'o' => "open",
344
                  'q' => "quit",
345
                  'h' => "help",
346
                  'l' => "list",
347
                  'a' => "add",
348
                  'g' => "generate",
349
                  's' => "save",
350
                  'p' => "parameter",
351
                  'd' => "delete",
352
                  'c' => "connect",
353
                  'x' => "exit"
354
                  }
355
 
356
    # all available commands
357
    @commands = %w[ new open list add parameter sparameter
358
                    delete connect save help quit exit
359
                    generate ]
360
 
361
    comp = proc { |s| (@cmd_list + Dir.entries( Dir.pwd )).grep( /^#{Regexp.escape(s)}/ ) }
362
    Readline.completion_append_character = " "
363
    Readline.completion_proc = comp
364
 
365
  end
366
 
367
  def run
368
 
369
    ##
370
    # process user commands
371
    #
372
    while buf = Readline.readline( "> ", true )
373
      process_cmd buf
374
    end
375
  end
376
 
377
  def process_cmd( c )
378
 
379
      # remove the comments and split each line
380
      match = SOCMaker::conf[ :COMMENT_REGEX ].match( c )
381
      cmd_arr = match[1].split( ' ' )
382
 
383
      # process the command, if there is one
384
      if cmd_arr.size > 0
385
        cmd     = ""
386
        if cmd_arr[ 0 ].size == 1 and @appr_map[ cmd_arr[ 0 ] ] != nil
387
          cmd = @appr_map[ cmd_arr[ 0 ] ]
388
        else
389
          cmd = cmd_arr[ 0 ]
390
        end
391
 
392
        if @commands.include?( cmd )
393
          cmd_str = "do_#{cmd}( cmd_arr[ 1..-1] )"
394
          puts "evaluating >>#{cmd_str}<< "
395
          eval( cmd_str )
396
        #TODO this is for linux only
397
        elsif system( "which #{cmd} > /dev/null 2>&1" )
398
          system( c )
399
        else
400
          puts "Command #{cmd} not available"
401
        end
402
       #begin
403
       #rescue
404
       #  puts "evaluating >>#{cmd_str}<< failed"
405
       #end
406
      end
407
  end
408
 
409
  @@inst = nil
410
 
411
 
412
end
413
end
414
 
415
 
416
# vim: noai:ts=2:sw=2
417
 

powered by: WebSVN 2.1.0

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