Module: HDLRuby::High::HExpression
- Defined in:
- lib/HDLRuby/hruby_high.rb,
lib/HDLRuby/std/channel.rb
Overview
Enhance expressions with possibility to act like a reading branch.
Instance Attribute Summary collapse
-
#systemT ⇒ Object
readonly
The system type the expression has been resolved in, if any.
-
#type ⇒ Object
The type of the expression if any.
Class Method Summary collapse
-
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
Instance Method Summary collapse
-
#[](rng) ⇒ Object
Creates an access to elements of range +rng+ of the signal.
-
#as(type) ⇒ Object
Casts as +type+.
-
#coerce(obj) ⇒ Object
Coerce by forcing convertion of obj to expression.
-
#constant? ⇒ Boolean
Tell if the expression is constant.
-
#inout(name) ⇒ Object
Creates inout port +name+ and connect it to the expression.
-
#input(name) ⇒ Object
Creates input port +name+ and connect it to the expression.
-
#ljust(n, v) ⇒ Object
Extends on the left to +n+ bits filling with +v+ bit values.
-
#lr(n) ⇒ Object
Left rotate of +n+ bits.
-
#ls(n) ⇒ Object
Left shift of +n+ bits.
-
#mux(*choices) ⇒ Object
Converts to a select operator using current expression as condition for one of the +choices+.
- #orig_operator(op) ⇒ Object
-
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
-
#read(target, &ruby_block) ⇒ Object
Transmits the expression to +target+ and execute +ruby_block+ if any.
-
#rjust(n, v) ⇒ Object
Extends on the right to +n+ bits filling with +v+ bit values.
-
#rr(n) ⇒ Object
Right rotate of +n+ bits.
-
#rs(n) ⇒ Object
Right shift of +n+ bits.
-
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
-
#to_bit ⇒ Object
Casts to a bit vector type.
-
#to_expr ⇒ Object
Converts to a new expression.
-
#to_unsigned ⇒ Object
Casts to a signed bit vector type.
-
#to_value ⇒ Object
Converts to a new value.
-
#to_value? ⇒ Boolean
Tell if the expression can be converted to a value.
-
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
Instance Attribute Details
#systemT ⇒ Object (readonly)
The system type the expression has been resolved in, if any.
2251 2252 2253 |
# File 'lib/HDLRuby/hruby_high.rb', line 2251 def systemT @systemT end |
#type ⇒ Object
The type of the expression if any.
2253 2254 2255 |
# File 'lib/HDLRuby/hruby_high.rb', line 2253 def type @type end |
Class Method Details
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
2391 2392 2393 |
# File 'lib/HDLRuby/hruby_high.rb', line 2391 def self.orig_operator(op) return (op.to_s + "_orig").to_sym end |
Instance Method Details
#[](rng) ⇒ Object
Creates an access to elements of range +rng+ of the signal.
NOTE: +rng+ can be a single expression in which case it is an index.
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 |
# File 'lib/HDLRuby/hruby_high.rb', line 2464 def [](rng) if rng.respond_to?(:to_expr) then # Number range: convert it to an expression. rng = rng.to_expr end if rng.is_a?(HDLRuby::Low::Expression) then # Index case return RefIndex.new(self.type.base,self.to_expr,rng) else # Range case, ensure it is made among expression. first = rng.first.to_expr last = rng.last.to_expr # Abd create the reference. return RefRange.new(self.type.slice(first..last), self.to_expr,first..last) end end |
#as(type) ⇒ Object
Casts as +type+.
2351 2352 2353 |
# File 'lib/HDLRuby/hruby_high.rb', line 2351 def as(type) return Cast.new(type.to_type,self.to_expr) end |
#coerce(obj) ⇒ Object
Coerce by forcing convertion of obj to expression.
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 |
# File 'lib/HDLRuby/hruby_high.rb', line 2434 def coerce(obj) if obj.is_a?(HDLRuby::Low::Expression) then # Already an expression, nothing to do. return [obj,self] elsif obj.respond_to?(:to_expr) then # Can be converted to an expression, do it. return [obj.to_expr, self] else return [obj,self] end end |
#constant? ⇒ Boolean
Tell if the expression is constant.
2329 2330 2331 2332 2333 2334 2335 |
# File 'lib/HDLRuby/hruby_high.rb', line 2329 def constant? # By default not constant. return false unless self.each_node.any? # If any sub node, check if all of them are constants. self.each_node { |node| return false unless node.constant? } return true end |
#inout(name) ⇒ Object
Creates inout port +name+ and connect it to the expression.
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 |
# File 'lib/HDLRuby/hruby_high.rb', line 2296 def inout(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the inout. port = nil HDLRuby::High.cur_system.open do port = obj.type.inout(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do RefObject.new(inst,port.to_ref) <= obj end end return port end |
#input(name) ⇒ Object
Creates input port +name+ and connect it to the expression.
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 |
# File 'lib/HDLRuby/hruby_high.rb', line 2256 def input(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the input. port = nil HDLRuby::High.cur_system.open do port = obj.type.input(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do RefObject.new(inst,port.to_ref) <= obj end end return port end |
#ljust(n, v) ⇒ Object
Extends on the left to +n+ bits filling with +v+ bit values.
2371 2372 2373 |
# File 'lib/HDLRuby/hruby_high.rb', line 2371 def ljust(n,v) return [(v.to_s * (n-self.width)).to_expr, self] end |
#lr(n) ⇒ Object
Left rotate of +n+ bits.
2422 2423 2424 2425 |
# File 'lib/HDLRuby/hruby_high.rb', line 2422 def lr(n) w = self.type.width return [self[w-(n+1)..0], self[w-1..w-(n)]] end |
#ls(n) ⇒ Object
Left shift of +n+ bits.
2412 2413 2414 |
# File 'lib/HDLRuby/hruby_high.rb', line 2412 def ls(n) return self << n end |
#mux(*choices) ⇒ Object
Converts to a select operator using current expression as condition for one of the +choices+.
NOTE: +choices+ can either be a list of arguments or an array. If +choices+ has only two entries (and it is not a hash), +value+ will be converted to a boolean.
2489 2490 2491 2492 2493 2494 2495 |
# File 'lib/HDLRuby/hruby_high.rb', line 2489 def mux(*choices) # Process the choices. choices = choices.flatten(1) if choices.size == 1 choices.map! { |choice| choice.to_expr } # Generate the select expression. return Select.new(choices[0].type,"?",self.to_expr,*choices) end |
#orig_operator(op) ⇒ Object
2394 2395 2396 |
# File 'lib/HDLRuby/hruby_high.rb', line 2394 def orig_operator(op) HExpression.orig_operator(op) end |
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 |
# File 'lib/HDLRuby/hruby_high.rb', line 2276 def output(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the output. port = nil HDLRuby::High.cur_system.open do port = obj.type.output(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do obj <= RefObject.new(inst,port.to_ref) end end return port end |
#read(target, &ruby_block) ⇒ Object
Transmits the expression to +target+ and execute +ruby_block+ if any.
884 885 886 887 |
# File 'lib/HDLRuby/std/channel.rb', line 884 def read(target,&ruby_block) target <= self ruby_block.call if ruby_block end |
#rjust(n, v) ⇒ Object
Extends on the right to +n+ bits filling with +v+ bit values.
2376 2377 2378 |
# File 'lib/HDLRuby/hruby_high.rb', line 2376 def rjust(n,v) return [self, (v.to_s * (n-self.width)).to_expr] end |
#rr(n) ⇒ Object
Right rotate of +n+ bits.
2428 2429 2430 2431 |
# File 'lib/HDLRuby/hruby_high.rb', line 2428 def rr(n) w = self.type.width return [self[(n-1)..0], self[w-1..n]] end |
#rs(n) ⇒ Object
Right shift of +n+ bits.
2417 2418 2419 |
# File 'lib/HDLRuby/hruby_high.rb', line 2417 def rs(n) return self >> n end |
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
2386 2387 2388 |
# File 'lib/HDLRuby/hruby_high.rb', line 2386 def sext(n) return self.ljust(self[-1]) end |
#to_bit ⇒ Object
Casts to a bit vector type.
2356 2357 2358 |
# File 'lib/HDLRuby/hruby_high.rb', line 2356 def to_bit return self.as(bit[self.width]) end |
#to_expr ⇒ Object
Converts to a new expression.
NOTE: to be redefined in case of non-expression class.
2341 2342 2343 |
# File 'lib/HDLRuby/hruby_high.rb', line 2341 def to_expr raise AnyError, "Internal error: to_expr not defined yet for class: #{self.class}" end |
#to_unsigned ⇒ Object
Casts to a signed bit vector type.
2361 2362 2363 |
# File 'lib/HDLRuby/hruby_high.rb', line 2361 def to_unsigned return self.as(unsigned[self.width]) end |
#to_value ⇒ Object
Converts to a new value.
NOTE: to be redefined.
2323 2324 2325 2326 |
# File 'lib/HDLRuby/hruby_high.rb', line 2323 def to_value raise AnyError, "Expression cannot be converted to a value: #{self.class}" end |
#to_value? ⇒ Boolean
Tell if the expression can be converted to a value.
2316 2317 2318 |
# File 'lib/HDLRuby/hruby_high.rb', line 2316 def to_value? return false end |
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
2381 2382 2383 |
# File 'lib/HDLRuby/hruby_high.rb', line 2381 def zext(n) return self.ljust(n,0) end |