Class: HDLRuby::High::Std::ChannelI

Inherits:
Object
  • Object
show all
Includes:
Hmissing
Defined in:
lib/HDLRuby/std/channel.rb

Overview

Describes a high-level channel instance.

Constant Summary

Constants included from Hmissing

Hmissing::High

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hmissing

#method_missing

Constructor Details

#initialize(name, &ruby_block) ⇒ ChannelI

Creates a new channel instance with +name+ built from +ruby_block+.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/HDLRuby/std/channel.rb', line 79

def initialize(name,&ruby_block)
    # Check and set the name
    @name = name.to_sym

    obj = self

    # The reader input ports by name.
    @reader_inputs = {}
    # The reader output ports by name.
    @reader_outputs = {}
    # The reader inout ports by name.
    @reader_inouts = {}

    # The writer input ports by name.
    @writer_inputs = {}
    # The writer output ports by name.
    @writer_outputs = {}
    # The writer inout ports by name.
    @writer_inouts = {}

    # Create the namespaces for building the channel, its readers
    # and its writers.

    # Creates the namespace of the channel.
    @channel_namespace = Namespace.new(self)
    # Make it give access to the internal of the class.
    @channel_namespace.add_method(:reader_input, &method(:reader_input))
    @channel_namespace.add_method(:reader_output,&method(:reader_output))
    @channel_namespace.add_method(:reader_inout, &method(:reader_inout))
    @channel_namespace.add_method(:writer_input, &method(:writer_input))
    @channel_namespace.add_method(:writer_output,&method(:writer_output))
    @channel_namespace.add_method(:writer_inout, &method(:writer_inout))
    @channel_namespace.add_method(:reader,       &method(:reader))
    @channel_namespace.add_method(:writer,       &method(:writer))

    # Creates the namespace of the reader.
    @reader_namespace = Namespace.new(self)
    # Creates the namespace of the writer.
    @writer_namespace = Namespace.new(self)

    # By default the namespace is the one of the namespace
    @namespace = @channel_namespace

    # Builds the channel.
    HDLRuby::High.space_push(@namespace)
    # puts "top_user=#{HDLRuby::High.top_user}"
    HDLRuby::High.top_user.instance_eval(&ruby_block)
    HDLRuby::High.space_pop

    # Gives access to the channel by registering its name.
    obj = self
    HDLRuby::High.space_reg(@name) { self }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HDLRuby::High::Hmissing

Instance Attribute Details

#nameObject (readonly)

The name of the channel instance.



72
73
74
# File 'lib/HDLRuby/std/channel.rb', line 72

def name
  @name
end

#namespaceObject (readonly)

The namespace associated with the current execution when building a channel, its reader or its writer.



76
77
78
# File 'lib/HDLRuby/std/channel.rb', line 76

def namespace
  @namespace
end

Instance Method Details

#command(name, &ruby_block) ⇒ Object

Defines new command +name+ to execute +ruby_block+ for the channel.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/HDLRuby/std/channel.rb', line 139

def command(name,&ruby_block)
    # Ensures name is a symbol.
    name = name.to_sym
    # Sets the new command.
    self.define_singleton_method(name) do
        # Executes the command in the right environment.
        HDLRuby::High.space_push(@namespace)
        res = HDLRuby::High.top_user.instance_exec(&ruby_block)
        HDLRuby::High.space_pop
        res
    end
end

#read(*args, &ruby_block) ⇒ Object

Performs a read on the channel using +args+ and +ruby_block+ as arguments.



288
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
# File 'lib/HDLRuby/std/channel.rb', line 288

def read(*args,&ruby_block)
    # Fill the reader namespace with the access to the reader signals.
    @reader_inputs.each do |name,sig|
        @reader_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    @reader_outputs.each do |name,sig|
        @reader_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    @reader_inouts.each do |name,sig|
        @reader_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    # Gain access to the reader as local variable.
    reader_proc = @reader_proc
    # The context is the one of the reader.
    @namespace = @reader_namespace
    # Execute the code generating the reader in context.
    HDLRuby::High.space_push(@namespace)
    HDLRuby::High.cur_block.open do
        instance_exec(ruby_block,*args,&reader_proc)
    end
    HDLRuby::High.space_pop
    # Restores the default context.
    @namespace = @channel_namespace
end

#reader(&ruby_block) ⇒ Object

Sets the read procedure to be +ruby_block+.



227
228
229
# File 'lib/HDLRuby/std/channel.rb', line 227

def reader(&ruby_block)
    @reader_proc = ruby_block
end

#reader_inout(*keys) ⇒ Object

Sets the signals accessible through +key+ to be reader inout port.



179
180
181
182
183
184
185
186
187
188
# File 'lib/HDLRuby/std/channel.rb', line 179

def reader_inout(*keys)
    # Registers each signal as reader port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @reader_inouts[name] = send(key)
    end
end

#reader_input(*keys) ⇒ Object

Sets the signals accessible through +key+ to be reader input port.



155
156
157
158
159
160
161
162
163
164
# File 'lib/HDLRuby/std/channel.rb', line 155

def reader_input(*keys)
    # Registers each signal as reader port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @reader_inputs[name] = send(key)
    end
end

#reader_output(*keys) ⇒ Object

Sets the signals accessible through +key+ to be reader output port.



167
168
169
170
171
172
173
174
175
176
# File 'lib/HDLRuby/std/channel.rb', line 167

def reader_output(*keys)
    # Registers each signal as reader port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @reader_outputs[name] = send(key)
    end
end

#reader_portsObject

Declares the ports for the reader.



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/HDLRuby/std/channel.rb', line 257

def reader_ports
    loc_inputs  = @reader_inputs
    loc_outputs = @reader_outputs
    loc_inouts  = @reader_inouts
    HDLRuby::High.cur_system.open do
        # The inputs
        loc_inputs.each  { |name,sig| sig.type.input  name }
        # The outputs
        loc_outputs.each { |name,sig| sig.type.output name }
        # The inouts
        loc_inouts.each  { |name,sig| sig.type.inout  name }
    end
end

#reader_signalsObject

Gets the list of the signals of the channel to be connected to the reader.



242
243
244
245
# File 'lib/HDLRuby/std/channel.rb', line 242

def reader_signals
    return @reader_inputs.values + @reader_outputs.values +
           @reader_inouts.values
end

#write(*args, &ruby_block) ⇒ Object

Performs a write on the channel using +args+ and +ruby_block+ as arguments.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/HDLRuby/std/channel.rb', line 321

def write(*args,&ruby_block)
    # Fill the writer namespace with the access to the writer signals.
    @writer_inputs.each do |name,sig|
        @writer_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    @writer_outputs.each do |name,sig|
        @writer_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    @writer_inouts.each do |name,sig|
        @writer_namespace.add_method(sig.name) do
            HDLRuby::High.top_user.send(name)
        end
    end
    # Gain access to the writer as local variable.
    writer_proc = @writer_proc
    # The context is the one of the writer.
    @namespace = @writer_namespace
    # Execute the code generating the writer in context.
    HDLRuby::High.space_push(@namespace)
    HDLRuby::High.cur_block.open do
        instance_exec(ruby_block,*args,&writer_proc)
    end
    HDLRuby::High.space_pop
    # Restores the default context.
    @namespace = @channel_namespace
end

#writer(&ruby_block) ⇒ Object

Sets the writter procedure to be +ruby_block+.



232
233
234
# File 'lib/HDLRuby/std/channel.rb', line 232

def writer(&ruby_block)
    @writer_proc = ruby_block
end

#writer_inout(*keys) ⇒ Object

Sets the signals accessible through +key+ to be writer inout port.



215
216
217
218
219
220
221
222
223
224
# File 'lib/HDLRuby/std/channel.rb', line 215

def writer_inout(*keys)
    # Registers each signal as writer port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @writer_inouts[name] = send(key)
    end
end

#writer_input(*keys) ⇒ Object

Sets the signals accessible through +key+ to be writer input port.



191
192
193
194
195
196
197
198
199
200
# File 'lib/HDLRuby/std/channel.rb', line 191

def writer_input(*keys)
    # Registers each signal as writer port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @writer_inputs[name] = send(key)
    end
end

#writer_output(*keys) ⇒ Object

Sets the signals accessible through +key+ to be writer output port.



203
204
205
206
207
208
209
210
211
212
# File 'lib/HDLRuby/std/channel.rb', line 203

def writer_output(*keys)
    # Registers each signal as writer port
    keys.each do |key|
        # Ensure the key is a symbol.
        key = key.to_sym
        # Register it with the corresponding signal.
        name = HDLRuby.uniq_name # The name of the signal is uniq.
        @writer_outputs[name] = send(key)
    end
end

#writer_portsObject

Declares the ports for the writer.



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/HDLRuby/std/channel.rb', line 272

def writer_ports
    loc_inputs  = @writer_inputs
    loc_outputs = @writer_outputs
    loc_inouts  = @writer_inouts
    HDLRuby::High.cur_system.open do
        # The inputs
        loc_inputs.each  { |name,sig| sig.type.input  name }
        # The outputs
        loc_outputs.each { |name,sig| sig.type.output name }
        # The inouts
        loc_inouts.each  { |name,sig| sig.type.inout  name }
    end
end

#writer_signalsObject

Gets the list of the signals of the channel to be connected to the writer.



249
250
251
252
# File 'lib/HDLRuby/std/channel.rb', line 249

def writer_signals
    return @writer_inputs.values + @writer_outputs.values +
           @writer_inouts.values
end