Module: HDLRuby::Vprocess

Included in:
High::Value
Defined in:
lib/HDLRuby/hruby_values.rb

Overview

To include to classes for value processing support.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.concat(*vals) ⇒ Object

Concat the content of +vals+.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/HDLRuby/hruby_values.rb', line 298

def self.concat(*vals)
    # Compute the resulting type.
    types = vals.map {|v| v.type }
    if types.uniq.count <= 1 then
        res_type = types[0][types.size]
    else
        res_type = vals.map {|v| v.type }.to_type
    end
    # Concat the contents.
    res_content = []
    content = width = 0
    vals.each_with_index do |val,i|
        content = val.content
        width = types[i].width
        if content.is_a?(BitString) then
            count = 0
            content.raw_content.each do |b|
                res_content << b
                count += 1
                break if count == width
            end
            if count < width then
                res_content.concat([res_content[-1]] * (width-count))
            end
        else
            width.times do |p|
                res_content << content[p]
            end
        end
    end
    # Make a bit string from res_content.
    res_content = BitString.new(res_content,:raw)
    # Return the resulting value.
    return vals[0].class.new(res_type,res_content)
end

Instance Method Details

#cast(type, nodir = false) ⇒ Object

Cast to +type+. NOTE: nodir tells if the direction is to be ignored.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/HDLRuby/hruby_values.rb', line 255

def cast(type,nodir = false)
    # Handle the direction.
    if !nodir && type.direction != self.type.direction then
        if self.content.is_a?(Numeric) then
            tmp = 0
            res_content = self.content
            self.type.width.times do |i|
                tmp = tmp*2 | (res_content & 1)
                res_content /= 2
            end
            res_content = tmp
        elsif self.content.is_a?(BitString) then
            res_content = self.content.clone.reverse!(self.type.width)
        else
            res_content = self.content.reverse
        end
    else
        res_content = self.content.clone
    end
    # Handle the sign.
    if type.unsigned? && !self.content.positive? then
        # Ensure the content is a positive value to match unsigned type.
        if res_content.is_a?(Numeric) then
            res_content &= ~(-1 << type.width) if res_content < 0
            # res_content &= ~(-1 * 2**type.width) if res_content < 0
        else
            res_content.positive!
        end
    end
    if type.signed && res_content.is_a?(Numeric) && res_content >= (1 << (type.width-1)) then
        res_content = (-1 << type.width) + res_content
    end
    # # truncs to the right size if necessary.
    # if res_content.is_a?(BitString) then
    #     res_content.trunc!(type.width)
    # else 
    #     res_content = self.trunc(res_content,type.width)
    # end
    # Generate the resulting value.
    return self.class.new(type,res_content)
end

#coerce(other) ⇒ Object

Coercion when operation from Ruby values.



382
383
384
# File 'lib/HDLRuby/hruby_values.rb', line 382

def coerce(other)
    return other,self.content
end

#eql?(val) ⇒ Boolean

Hash-map comparison of values. Also use in simulation engines to know if a signal changed.

Returns:

  • (Boolean)


388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/HDLRuby/hruby_values.rb', line 388

def eql?(val)
    if self.content.is_a?(Numeric) then
        return self.content == val if val.is_a?(Numeric)
        return self.content == val.content if val.content.is_a?(Numeric)
        return false unless val.content.specified?
        return self.content == val.content.to_i
    else
        return self.content.to_i == val if val.is_a?(Numeric)
        return self.content.eql?(val.content) unless val.content.is_a?(Numeric)
        return false if self.content.specified?
        return self.content.to_i == val.content
    end
end

#impedence?Boolean

Tell if the value is high impedance.

Returns:

  • (Boolean)


414
415
416
417
# File 'lib/HDLRuby/hruby_values.rb', line 414

def impedence?
    return false unless @content.is_a?(BitString)
    return @content.raw_content.include?(2)
end

#to_fObject

Conversion to a float if possible.



350
351
352
# File 'lib/HDLRuby/hruby_values.rb', line 350

def to_f
    return self.content.to_f
end

#to_iObject

Conversion to an integer if possible.



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/HDLRuby/hruby_values.rb', line 336

def to_i
    # if self.content.is_a?(BitString) then
    #     if self.type.signed? then
    #         return self.content.to_numeric_signed
    #     else
    #         return self.content.to_numeric
    #     end
    # else
    #     return self.content.to_i
    # end
    return self.content.to_i
end

#to_vstrObject

Converts the value to a string of the right size.



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/HDLRuby/hruby_values.rb', line 420

def to_vstr
    if self.content.is_a?(Numeric) then
        if self.content >= 0 then 
            str = "0" + self.content.to_s(2)
        else
            str = (2**((-self.content).width+1) + self.content).to_s(2)
        end
    else
        str = self.content.to_s
    end
    width = self.type.width
    if str.size >= width then
        return str[-width..-1]
    else
        return str[0] * (width-str.size) + str
    end
end

#trunc(val, width) ⇒ Object

Truncs integer +val+ to +width+



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/HDLRuby/hruby_values.rb', line 15

def trunc(val,width)
    if val.is_a?(BitString) then
        return val[(width-1)..0]
    end
    if val.bit_length > width then
        if val >= 0 then
            # return val & (2**width-1)
            return val % (2**width)
            # return val & TRUNC_P_T[width]
        else
            # return val | (-1 * 2**width)
            return val % -(2**width)
            # return val | TRUNC_N_T[width]
        end
    else
        return val
    end
end

#zero?Boolean

Tell if the value is zero.

Returns:

  • (Boolean)


404
405
406
407
408
409
410
411
# File 'lib/HDLRuby/hruby_values.rb', line 404

def zero?
    return false unless @content
    if content.is_a?(Numeric) then
        return @content & (2**self.type.width-1) == 0
    else
        return !@content.raw_content[0..self.type.width-1].any?{|b| b!=0}
    end
end