Class: OrigenMemoryImage::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/origen_memory_image/base.rb

Direct Known Subclasses

Binary, Hex, IntelHex, SRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
# File 'lib/origen_memory_image/base.rb', line 5

def initialize(file, options = {})
  if options[:source] == String
    @source = file
  else
    @file = file
  end
  @ljust_partial_data = options[:ljust_partial_data]
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/origen_memory_image/base.rb', line 3

def file
  @file
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/origen_memory_image/base.rb', line 3

def source
  @source
end

Instance Method Details

#file_nameObject



84
85
86
# File 'lib/origen_memory_image/base.rb', line 84

def file_name
  file || 'From source string'
end

#flip_endianness(data, width_in_bytes) ⇒ Object

Reverse the endianness of the given data value, the width of it in bytes must be supplied as the second argument

Examples:

flip_endianness(0x12345678, 4)  # => 0x78563412


74
75
76
77
78
79
80
81
82
# File 'lib/origen_memory_image/base.rb', line 74

def flip_endianness(data, width_in_bytes)
  v = 0
  width_in_bytes.times do |i|
    # data[7:0] => data[15:8]
    start = 8 * i
    v += data[(start + 7)..start] << ((width_in_bytes - i - 1) * 8)
  end
  v
end

#has_start_recordObject

Returns true if a start (jump address) record exists



20
21
22
23
24
# File 'lib/origen_memory_image/base.rb', line 20

def has_start_record
  start_address unless @start_address
  @start_record_found = false if @start_record_found.nil?
  @start_record_found
end

#linesObject



88
89
90
91
92
93
94
# File 'lib/origen_memory_image/base.rb', line 88

def lines
  if file
    File.readlines(file)
  else
    source.split("\n")
  end
end

#start_addressObject

Returns the code execution start address as an int



15
16
17
# File 'lib/origen_memory_image/base.rb', line 15

def start_address
  fail "#{self.class} has not implemented the start_address method!"
end

#to_a(options = {}) ⇒ Object Also known as: to_array

Returns the s-record as an array of addresses and data

The output is a 2D array, with each element being an array with element zero being the address of the data and element one being one word of data like this [[ADDR0, DATA0], [ADDR1, DATA1], [ADDR2, DATA2]…]

The block header data and end of block value are not interpreted in any way and the checksum bits are disregarded

Parameters:

  • options, (hash)

    allows the selection of endianness swapping - ie the output will have the endianness changed



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/origen_memory_image/base.rb', line 36

def to_a(options = {})
  options = {
    flip_endianness:     false,
    data_width_in_bytes: 4,
    crop:                []
  }.merge(options)
  data = extract_addr_data(options)

  if options[:crop].count > 0
    cropped_data = []
    data.each do |addr, data|
      case options[:crop].count
        when 1
          cropped_data.push([addr, data]) if addr >= options[:crop][0]
        when 2
          cropped_data.push([addr, data]) if addr >= options[:crop][0] && addr <= options[:crop][1]
        else
          fail 'crop option can only be array of size 1 or 2'
      end
    end
    data = cropped_data
  end

  if options[:flip_endianness] || options[:endianness_change]
    data.map do |v|
      [v[0], flip_endianness(v[1], options[:data_width_in_bytes])]
    end
  else
    data
  end
end