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
-
.concat(*vals) ⇒ Object
Concat the content of +vals+.
Instance Method Summary collapse
-
#cast(type, nodir = false) ⇒ Object
Cast to +type+.
-
#coerce(other) ⇒ Object
Coercion when operation from Ruby values.
-
#eql?(val) ⇒ Boolean
Hash-map comparison of values.
-
#to_f ⇒ Object
Conversion to a float if possible.
-
#to_i ⇒ Object
Conversion to an integer if possible.
-
#to_vstr ⇒ Object
Converts the value to a string of the right size.
-
#trunc(val, width) ⇒ Object
Truncs integer +val+ to +width+.
-
#zero? ⇒ Boolean
Tell if the value is zero.
Class Method Details
.concat(*vals) ⇒ Object
Concat the content of +vals+.
289 290 291 292 293 294 295 296 297 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 |
# File 'lib/HDLRuby/hruby_values.rb', line 289 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.
249 250 251 252 253 254 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 |
# File 'lib/HDLRuby/hruby_values.rb', line 249 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 # # 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.
373 374 375 |
# File 'lib/HDLRuby/hruby_values.rb', line 373 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.
379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/HDLRuby/hruby_values.rb', line 379 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 |
#to_f ⇒ Object
Conversion to a float if possible.
341 342 343 |
# File 'lib/HDLRuby/hruby_values.rb', line 341 def to_f return self.content.to_f end |
#to_i ⇒ Object
Conversion to an integer if possible.
327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/HDLRuby/hruby_values.rb', line 327 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_vstr ⇒ Object
Converts the value to a string of the right size.
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/HDLRuby/hruby_values.rb', line 405 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 |
# File 'lib/HDLRuby/hruby_values.rb', line 15 def trunc(val,width) 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.
395 396 397 398 399 400 401 402 |
# File 'lib/HDLRuby/hruby_values.rb', line 395 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 |