Class: Adept::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/adept/core.rb

Overview

Class which represents a data-2-mem configurable IP core.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, shortname, base_path, targets) ⇒ Core

Initializes a new instance of a Core object.



70
71
72
73
74
75
# File 'lib/adept/core.rb', line 70

def initialize(name, shortname, base_path, targets)
  @name = name
  @shortname = shortname
  @base_path = base_path
  @targets = targets
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/adept/core.rb', line 13

def name
  @name
end

#shortnameObject (readonly)

Returns the value of attribute shortname.



15
16
17
# File 'lib/adept/core.rb', line 15

def shortname
  @shortname
end

#targetsObject (readonly)

Returns the value of attribute targets.



14
15
16
# File 'lib/adept/core.rb', line 14

def targets
  @targets
end

Class Method Details

.available_cores(device = nil) ⇒ Object

Returns a list of all available cores, optionally filtered by a device ID string.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/adept/core.rb', line 20

def self.available_cores(device=nil)

  #Determine the path at which the ruby-adept gem is stored.
  gem_path = File.expand_path('../..', File.dirname(__FILE__))

  #Get a list of all available core definition files.
  core_definitions = Dir["#{gem_path}/cores/**/core.yaml"]

  #Convert each definition to a YAML core.
  definitions = core_definitions.map { |file| from_definition_file(file) }

  #If we were passed a device shortname to filter by, ensure we
  #only return cores that support that target.
  unless device.nil?
    definitions = definitions.select { |core| core.targets.include?(device) }
  end

  definitions

end

.from_definition_file(file) ⇒ Object

Creates a new Core object from a YAML definition file.

file: The full path to a YAML core definition file.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/adept/core.rb', line 54

def self.from_definition_file(file)

  #Get the path of the directory that holds the core's files.
  base_path = File.dirname(file)

  #Parse the YAML definition file.
  raw_definition = YAML.load_file(file)

  #Return a new core object. 
  new(raw_definition["name"], raw_definition["shortname"], base_path, raw_definition["targets"])

end

.from_shortname(shortname) ⇒ Object

Returns the core with the given shortname, or nil if no such core exists.



45
46
47
# File 'lib/adept/core.rb', line 45

def self.from_shortname(shortname)
  available_cores.find { |core| core.shortname == shortname }
end

Instance Method Details

#inspectObject

Print a debugging represntation of the core.



123
124
125
# File 'lib/adept/core.rb', line 123

def inspect
  "<IP Core 0x#{object_id.to_s(16)}: #@name>"
end

#to_bitstream(target = @targets.keys.first, program = nil, prom = false) ⇒ Object

Returns a bitstream which can be use to load the given core onto the target

device. If a program is provided, it will be loaded into the resultant bitstream.

target: The Device ID string of the board to be programmed.
program: The program that should be merged into the core.
prom: If true, the PROM version of this core will be used.


116
117
118
# File 'lib/adept/core.rb', line 116

def to_bitstream(target=@targets.keys.first, program=nil, prom=false)
  Adept::DataFormats::Bitstream.from_string(to_raw_bitstream(target, program, prom))
end

#to_raw_bitstream(target = @targets.keys.first, program = nil, prom = false) ⇒ Object

Returns a byte-string encoded bitstream which can be use to load the given core

onto the target device. If a program is provided, it will be loaded into the resultant bitstream.

target: The Device ID string of the board to be programmed.
program: The program that should be merged into the core.
prom: If true, the PROM version of this core will be used.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/adept/core.rb', line 85

def to_raw_bitstream(target=@targets.keys.first, program=nil, prom=false)

  #Ensure the target is a string.
  target = target.to_s

  #If we're in PROM mode, use the prom versions of the configured files.
  suffix = prom ? '_prom' : '';

  #Get the path to the bitfile and memory map which will be used to generate the new bitfile.
  memory_map = "#@base_path/#{@targets[target]["memory_map#{suffix}"]}"
  bit_file   = "#@base_path/#{@targets[target]["bit_file#{suffix}"]}"

  #If no program was provided, return the bitfile unmodified.
  return Adept::DataFormats::Bitstream.from_file(bit_file) unless program

  #Generate the new raw bitfile...
  hex = with_temporary_files                      { |dest, _|      system("avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock #{program} #{dest}") } 
  mem = with_temporary_files(hex, '.mem', '.hex') { |dest, source| system("srec_cat #{source} -Intel -Byte_Swap 2 -Data_Only -Output_Block_Size 32 -o #{dest} -vmem 8") }
  bit = with_temporary_files(mem, '.bit', '.mem') { |dest, source| system("data2mem -bm #{memory_map} -bt #{bit_file} -bd #{source} -o b #{dest}") }


end