Class: HDLRuby::Low::Truncers

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeTruncers

Returns a new instance of Truncers.



68
69
70
# File 'lib/HDLRuby/hruby_verilog.rb', line 68

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.



78
79
80
81
82
83
# File 'lib/HDLRuby/hruby_verilog.rb', line 78

def add(rngI,rngS)
    # Convert the ranges to arrays.
    rngI,rngS = self.r2a(rngI), self.r2a(rngS)
    # Add them
    @truncers << [rngI,rngS]
end

#dumpObject

Generate the truncating functionds.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/HDLRuby/hruby_verilog.rb', line 96

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.



73
74
75
# File 'lib/HDLRuby/hruby_verilog.rb', line 73

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.



88
89
90
91
92
93
# File 'lib/HDLRuby/hruby_verilog.rb', line 88

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