Class: HDLRuby::Low::Truncers
- Inherits:
-
Object
- Object
- HDLRuby::Low::Truncers
- Defined in:
- lib/HDLRuby/hruby_verilog.rb
Overview
Class for generating the truncating functions in verilog. Such function are necessary as expression cannot be truncated directly.
Instance Method Summary collapse
-
#add(rngI, rngS) ⇒ Object
(also: #<<)
Add a truncer to of expression of bit range +rngI+ using +rngS+ slice.
-
#dump ⇒ Object
Generate the truncating functionds.
-
#initialize ⇒ Truncers
constructor
A new instance of Truncers.
-
#r2a(rng) ⇒ Object
Convert a range to an array.
-
#truncer_name(rngI, rngS) ⇒ Object
Generate a truncer function name for expression of bit range +rngI+ using +rngS+ slice.
Constructor Details
#initialize ⇒ Truncers
Returns a new instance of Truncers.
66 67 68 |
# File 'lib/HDLRuby/hruby_verilog.rb', line 66 def initialize @truncers = [] end |
Instance Method Details
#add(rngI, rngS) ⇒ Object Also known as: <<
Add a truncer to of expression of bit range +rngI+ using +rngS+ slice.
76 77 78 79 80 81 |
# File 'lib/HDLRuby/hruby_verilog.rb', line 76 def add(rngI,rngS) # Convert the ranges to arrays. rngI,rngS = self.r2a(rngI), self.r2a(rngS) # Add them @truncers << [rngI,rngS] end |
#dump ⇒ Object
Generate the truncating functionds.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/HDLRuby/hruby_verilog.rb', line 94 def dump # Ensure there is only one truncating function per range. @truncers.sort!.uniq! # Generate the resulting code. codeT = "" @truncers.each do |(rngI,rngS)| rngO = [rngS[0]-rngS[1],0] codeT << " function [#{rngO[0]}:#{rngO[1]}] " codeT << self.truncer_name(rngI,rngS) codeT << "(input [#{rngI[0]}:#{rngI[1]}] val);\n" codeT << " " << self.truncer_name(rngI,rngS) << " = " codeT << "val[#{rngS[0]}:#{rngS[1]}];\n" codeT << " endfunction\n\n" end # Clears the truncers. @truncers = [] return codeT end |
#r2a(rng) ⇒ Object
Convert a range to an array.
71 72 73 |
# File 'lib/HDLRuby/hruby_verilog.rb', line 71 def r2a(rng) return [rng.first,rng.last] end |
#truncer_name(rngI, rngS) ⇒ Object
Generate a truncer function name for expression of bit range +rngI+ using +rngS+ slice.
86 87 88 89 90 91 |
# File 'lib/HDLRuby/hruby_verilog.rb', line 86 def truncer_name(rngI,rngS) # Convert the ranges to arrays. rngI,rngS = self.r2a(rngI), self.r2a(rngS) # Generate the name. return "trunc_#{rngI[0]}_#{rngI[1]}_#{rngS[0]}_#{rngS[1]}" end |