HDLRuby 3.6.2 → 3.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3513 @@
1
+ require "hruby_types.rb"
2
+
3
+ # Note: Fiber in C: https://github.com/higan-emu/libco
4
+
5
+ module RubyHDL
6
+ end
7
+
8
+ module RubyHDL::High
9
+
10
+ ##
11
+ # SW implementation of the Standard HDLRuby::High library:
12
+ # sequencer generator.
13
+ # The idea is to be able to write sw-like sequential code.
14
+ #
15
+ ########################################################################
16
+
17
+
18
+ @@absoluteCounter = -1 # The absolute name counter.
19
+
20
+ @@uniq_names = Set.new(Symbol.all_symbols.map {|sym| sym.to_s})
21
+
22
+ # Generates an absolute uniq name.
23
+ def self.uniq_name(base = "")
24
+ @@absoluteCounter += 1
25
+ name = base.to_s + ":#{@@absoluteCounter}"
26
+ if @@uniq_names.include?(name) then
27
+ # The symbol exists, try again.
28
+ return self.uniq_name
29
+ else
30
+ @@uniq_names.add(name)
31
+ return name.to_sym
32
+ end
33
+ end
34
+
35
+
36
+
37
+ # Describes a SW implementation of the top block.
38
+ # It cannot contain any statement, but can register objects.
39
+ class SblockTop
40
+ using RubyHDL::High
41
+
42
+ # Create a new top block.
43
+ def initialize
44
+ # Initialize the table of callable procs from outside.
45
+ @callables = { }
46
+ # Initialize the list of signals declared in this block.
47
+ @signals = []
48
+ end
49
+
50
+ # The sequencer of the block: none since top Sblock for globals.
51
+ def sequencer
52
+ nil
53
+ end
54
+
55
+ # Generate inner signals with type +type+ and names from +names+ list.
56
+ def make_inners(type,*names)
57
+ puts "make_inners with names=#{names.join(",")}"
58
+ type = type.to_type
59
+ last_sig = nil
60
+ names.each do |name|
61
+ name = name.to_sym
62
+ # Create and add the signal.
63
+ sig = SignalI.new(type,name)
64
+ @signals << sig
65
+ # Register it.
66
+ # self.register(name) { puts("sig=",sig.inspect); sig }
67
+ self.register(name) { sig }
68
+ last_sig = sig
69
+ end
70
+ return last_sig
71
+ end
72
+
73
+ # Register a new object named +name+ generated by +ruby_block+.
74
+ def register(name,&ruby_block)
75
+ # Add a way to call it from the stack of SW blocks.
76
+ ::Object.define_method(name) do
77
+ RubyHDL::High.call_sblock(name)
78
+ end
79
+ # Add a method for accessing the object.
80
+ # self.define_singleton_method(name,&ruby_block)
81
+ res = ruby_block.call
82
+ @callables[name] = ruby_block
83
+ end
84
+
85
+ # Tell if a method is callable from there.
86
+ def callable?(m)
87
+ return @callables.key?(m)
88
+ end
89
+
90
+ # Call a method from there.
91
+ def callable(m,*args,&ruby_block)
92
+ return @callables[m].call(*args,&ruby_block)
93
+ end
94
+
95
+ # Iterate on the signal declared in the block.
96
+ def each_signal(&ruby_block)
97
+ return to_enum(:each_signal) unless ruby_block
98
+ @signals.each(&ruby_block)
99
+ end
100
+ end
101
+
102
+
103
+ # The stack of SW blocks.
104
+ SBLOCK_STACK = [ SblockTop.new ]
105
+
106
+ def self.global_sblock
107
+ SBLOCK_STACK[0]
108
+ end
109
+
110
+ def self.top_sblock
111
+ SBLOCK_STACK[-1]
112
+ end
113
+
114
+ def self.push_sblock(sblock)
115
+ SBLOCK_STACK << sblock
116
+ end
117
+
118
+ def self.pop_sblock
119
+ SBLOCK_STACK.pop
120
+ end
121
+
122
+ # Calling a method from the stack.
123
+ def self.call_sblock(m,*args,&ruby_block)
124
+ SBLOCK_STACK.reverse_each do |sblock|
125
+ if sblock.callable?(m) then
126
+ # return sblock.callable(m,*args,&ruby_block)
127
+ res = sblock.callable(m,*args,&ruby_block)
128
+ return res
129
+ end
130
+ end
131
+ # Method not found.
132
+ method_missing(m,*args,&ruby_block)
133
+ end
134
+
135
+ # # Handling of new names.
136
+ # class ::Object
137
+ # alias_method :old_method_missing, :method_missing
138
+
139
+ # def method_missing(m, *args, &ruby_block)
140
+ # # print "method_missing in class=#{self.class} with m=#{m}\n"
141
+ # # Not a value, but maybe it is in the stack of SW blocks
142
+ # SBLOCK_STACK.reverse_each do |sblock|
143
+ # if sblock.respond_to?(m) then
144
+ # return sblock.send(m,*args,&ruby_block)
145
+ # end
146
+ # end
147
+ # # puts "here: #{m}"
148
+ # # No, true error
149
+ # self.old_method_missing(m,*args,&ruby_block)
150
+ # end
151
+ # end
152
+
153
+
154
+ # The translation of operators into Ruby code.
155
+ RUBY_OPERATOR = {
156
+ # Unary operators.
157
+ :"-@" => "-(%s)", :"+@" => "+(%s)", :"~" => "~(%s)",
158
+ :abs => "(%s).abs",
159
+ :boolean => "%s", :bit => "%s",
160
+ :signed => "%s", :unsigned => "(%s) & 0xFFFFFFFFFFFFFFFF",
161
+
162
+ # Binary operators.
163
+ :"+" => "(%s)+(%s)", :"-" => "(%s)-(%s)", :"*" => "(%s)*(%s)",
164
+ :"/" => "(%s)/(%s)", :"%" => "(%s)%%(%s)", :"**" => "(%s)**(%s)",
165
+ :"&" => "(%s)&(%s)", :"|" => "(%s)|(%s)", :"^" => "(%s)^(%s)",
166
+ :"<<" => "(%s)<<(%s)", :">>" => "(%s)>>(%s)",
167
+ :"==" => "(%s)==(%s)", :"!=" => "(%s)!=(%s)",
168
+ :"<" => "(%s)<(%s)", :">" => "(%s)>(%s)",
169
+ :"<=" => "(%s)<=(%s)",:">=" => "(%s)>=(%s)"
170
+ }
171
+
172
+ # The translation of operators into C code.
173
+ C_OPERATOR = {
174
+ # Unary operators.
175
+ :"-@" => "-(%s)", :"+@" => "+(%s)", :"~" => "~(%s)",
176
+ :abs => "(%s).abs",
177
+ :boolean => "%s", :bit => "%s",
178
+ :signed => "%s", :unsigned => "(%s) & 0xFFFFFFFFFFFFFFFF",
179
+
180
+ # Binary operators.
181
+ :"+" => "(%s)+(%s)", :"-" => "(%s)-(%s)", :"*" => "(%s)*(%s)",
182
+ :"/" => "(%s)/(%s)", :"%" => "(%s)%%(%s)", :"**" => "pow((%s),(%s))",
183
+ :"&" => "(%s)&(%s)", :"|" => "(%s)|(%s)", :"^" => "(%s)^(%s)",
184
+ :"<<" => "(%s)<<(%s)", :">>" => "(%s)>>(%s)",
185
+ :"==" => "(%s)==(%s)", :"!=" => "(%s)!=(%s)",
186
+ :"<" => "(%s)<(%s)", :">" => "(%s)>(%s)",
187
+ :"<=" => "(%s)<=(%s)",:">=" => "(%s)>=(%s)"
188
+ }
189
+
190
+
191
+
192
+ # Module adding functionalities to object including the +seach+ method.
193
+ module SEnumerable
194
+
195
+ # Iterator on each of the elements in range +rng+.
196
+ # *NOTE*:
197
+ # - Stop iteration when the end of the range is reached or when there
198
+ # are no elements left
199
+ # - This is not a method from Ruby but one specific for hardware where
200
+ # creating a array is very expensive.
201
+ def seach_range(rng,&ruby_block)
202
+ self.seach.seach_range(rng,&ruby_block)
203
+ end
204
+
205
+ # Tell if all the elements respect a given criterion given either
206
+ # as +arg+ or as block.
207
+ def sall?(arg = nil,&ruby_block)
208
+ self.seach.sall?(arg,&ruby_block)
209
+ end
210
+
211
+ # Tell if any of the elements respects a given criterion given either
212
+ # as +arg+ or as block.
213
+ def sany?(arg = nil,&ruby_block)
214
+ self.seach.sany?(arg,&ruby_block)
215
+ end
216
+
217
+ # Returns an SEnumerator generated from current enumerable and +arg+
218
+ def schain(arg)
219
+ self.seach.schain(arg)
220
+ end
221
+
222
+ # HW implementation of the Ruby chunk.
223
+ # NOTE: to do, or may be not.
224
+ def schunk(*args,&ruby_block)
225
+ raise "schunk is not supported yet."
226
+ end
227
+
228
+ # HW implementation of the Ruby chunk_while.
229
+ # NOTE: to do, or may be not.
230
+ def schunk_while(*args,&ruby_block)
231
+ raise "schunk_while is not supported yet."
232
+ end
233
+
234
+ # Returns a vector containing the execution result of the given block
235
+ # on each element. If no block is given, return an SEnumerator.
236
+ # NOTE: be carful that the resulting vector can become huge if there
237
+ # are many element.
238
+ def smap(&ruby_block)
239
+ self.seach.smap(&ruby_block)
240
+ end
241
+
242
+ # HW implementation of the Ruby flat_map.
243
+ # NOTE: actually due to the way HDLRuby handles vectors, should work
244
+ # like smap
245
+ def sflat_map(&ruby_block)
246
+ self.seach.sflat_map(&ruby_block)
247
+ end
248
+
249
+ # HW implementation of the Ruby compact, but remove 0 values instead
250
+ # on nil (since nil that does not have any meaning in HW).
251
+ def scompact
252
+ self.seach.scompact(&ruby_block)
253
+ end
254
+
255
+
256
+ # WH implementation of the Ruby count.
257
+ def scount(obj = nil, &ruby_block)
258
+ self.seach.scount(obj,&ruby_block)
259
+ end
260
+
261
+ # HW implementation of the Ruby cycle.
262
+ def scycle(n = nil,&ruby_block)
263
+ self.seach.scycle(n,&ruby_block)
264
+ end
265
+
266
+ # HW implementation of the Ruby find.
267
+ # NOTE: contrary to Ruby, if_none_proc is mandatory since there is no
268
+ # nil in HW. Moreover, the argument can also be a value.
269
+ def sfind(if_none_proc, &ruby_block)
270
+ self.seach.sfind(if_non_proc,&ruby_block)
271
+ end
272
+
273
+ # HW implementation of the Ruby drop.
274
+ def sdrop(n)
275
+ self.seach.sdrop(n)
276
+ end
277
+
278
+ # HW implementation of the Ruby drop_while.
279
+ def sdrop_while(&ruby_block)
280
+ self.seach.sdrop_while(n)
281
+ end
282
+
283
+ # HW implementation of the Ruby each_cons
284
+ def seach_cons(n,&ruby_block)
285
+ self.seach.seach_cons(n,&ruby_block)
286
+ end
287
+
288
+ # HW implementation of the Ruby each_entry.
289
+ # NOTE: to do, or may be not.
290
+ def seach_entry(*args,&ruby_block)
291
+ raise "seach_entry is not supported yet."
292
+ end
293
+
294
+ # HW implementation of the Ruby each_slice
295
+ def seach_slice(n,&ruby_block)
296
+ self.seach.seach_slice(n,&ruby_block)
297
+ end
298
+
299
+ # HW implementation of the Ruby each_with_index.
300
+ def seach_with_index(*args,&ruby_block)
301
+ self.seach.swith_index(*args,&ruby_block)
302
+ end
303
+
304
+ # HW implementation of the Ruby each_with_object.
305
+ def seach_with_object(obj,&ruby_block)
306
+ self.seach.swith_object(obj,&ruby_block)
307
+ end
308
+
309
+ # HW implementation of the Ruby to_a.
310
+ def sto_a
311
+ self.seach.sto_a
312
+ end
313
+
314
+ # HW implementation of the Ruby select.
315
+ def sselect(&ruby_block)
316
+ self.seach.sselect(&ruby_block)
317
+ end
318
+
319
+ # HW implementation of the Ruby find_index.
320
+ def sfind_index(obj = nil, &ruby_block)
321
+ self.seach.sfind_index(obj,&ruby_block)
322
+ end
323
+
324
+ # HW implementation of the Ruby first.
325
+ def sfirst(n=1)
326
+ self.seach.sfirst(n)
327
+ end
328
+
329
+ # HW implementation of the Ruby grep.
330
+ # NOTE: to do, or may be not.
331
+ def sgrep(*args,&ruby_block)
332
+ raise "sgrep is not supported yet."
333
+ end
334
+
335
+ # HW implementation of the Ruby grep_v.
336
+ # NOTE: to do, or may be not.
337
+ def sgrep_v(*args,&ruby_block)
338
+ raise "sgrep_v is not supported yet."
339
+ end
340
+
341
+ # HW implementation of the Ruby group_by.
342
+ # NOTE: to do, or may be not.
343
+ def sgroup_by(*args,&ruby_block)
344
+ raise "sgroup_by is not supported yet."
345
+ end
346
+
347
+ # HW implementation of the Ruby include?
348
+ def sinclude?(obj)
349
+ return self.seach.sinclude?(obj)
350
+ end
351
+
352
+ # HW implementation of the Ruby inject.
353
+ def sinject(*args,&ruby_block)
354
+ return self.seach.sinject(*args,&ruby_block)
355
+ end
356
+
357
+ # HW implementation of the Ruby reduce.
358
+ def sreduce(*args,&ruby_block)
359
+ return self.seach.sreduce(*args,&ruby_block)
360
+ end
361
+
362
+ # HW implementation of the Ruby lazy.
363
+ # NOTE: to do, or may be not.
364
+ def slazy(*args,&ruby_block)
365
+ raise "slazy is not supported yet."
366
+ end
367
+
368
+ # HW implementation of the Ruby max.
369
+ def smax(n = nil, &ruby_block)
370
+ return self.seach.smax(n,&ruby_block)
371
+ end
372
+
373
+ # HW implementation of the Ruby max_by.
374
+ def smax_by(n = nil, &ruby_block)
375
+ return self.seach.smax_by(n,&ruby_block)
376
+ end
377
+
378
+ # HW implementation of the Ruby min.
379
+ def smin(n = nil, &ruby_block)
380
+ return self.seach.smin(n,&ruby_block)
381
+ end
382
+
383
+ # HW implementation of the Ruby min_by.
384
+ def smin_by(n = nil, &ruby_block)
385
+ return self.seach.smin_by(n,&ruby_block)
386
+ end
387
+
388
+ # HW implementation of the Ruby minmax.
389
+ def sminmax(&ruby_block)
390
+ return self.seach.sminmax(&ruby_block)
391
+ end
392
+
393
+ # HW implementation of the Ruby minmax_by.
394
+ def sminmax_by(&ruby_block)
395
+ return self.seach.sminmax_by(&ruby_block)
396
+ end
397
+
398
+ # Tell if none of the elements respects a given criterion given either
399
+ # as +arg+ or as block.
400
+ def snone?(arg = nil,&ruby_block)
401
+ return self.seach.snone?(arg,&ruby_block)
402
+ end
403
+
404
+ # Tell if one and only one of the elements respects a given criterion
405
+ # given either as +arg+ or as block.
406
+ def sone?(arg = nil,&ruby_block)
407
+ return self.seach.sone?(arg,&ruby_block)
408
+ end
409
+
410
+ # HW implementation of the Ruby partition.
411
+ # NOTE: to do, or may be not.
412
+ def spartition(*args,&ruby_block)
413
+ raise "spartition is not supported yet."
414
+ end
415
+
416
+ # HW implementatiob of the Ruby reject.
417
+ def sreject(&ruby_block)
418
+ return self.seach.sreject(&ruby_block)
419
+ end
420
+
421
+ # HW implementatiob of the Ruby reverse_each.
422
+ def sreverse_each(*args,&ruby_block)
423
+ return self.seach.sreverse_each(*args,&ruby_block)
424
+ end
425
+
426
+ # HW implementation of the Ruby slice_after.
427
+ # NOTE: to do, or may be not.
428
+ def sslice_after(pattern = nil,&ruby_block)
429
+ raise "sslice_after is not supported yet."
430
+ end
431
+
432
+ # HW implementation of the Ruby slice_before.
433
+ # NOTE: to do, or may be not.
434
+ def sslice_before(*args,&ruby_block)
435
+ raise "sslice_before is not supported yet."
436
+ end
437
+
438
+ # HW implementation of the Ruby slice_when.
439
+ # NOTE: to do, or may be not.
440
+ def sslice_when(*args,&ruby_block)
441
+ raise "sslice_before is not supported yet."
442
+ end
443
+
444
+ # Merge two arrays in order, for ssort only.
445
+ def ssort_merge(arI, arO, first, middle, last, &ruby_block)
446
+ return self.seach.ssort_merge(arI,arO,first,middle,last,&ruby_block)
447
+ end
448
+
449
+ # HW implementation of the Ruby sort.
450
+ def ssort(&ruby_block)
451
+ return self.seach.ssort(&ruby_block)
452
+ end
453
+
454
+ # HW implementation of the Ruby sort.
455
+ def ssort_by(&ruby_block)
456
+ return self.seach.ssort_by(&ruby_block)
457
+ end
458
+
459
+ # HW implementation of the Ruby sum.
460
+ def ssum(initial_value = nil,&ruby_block)
461
+ return self.seach.ssum(initial_value,&ruby_block)
462
+ end
463
+
464
+ # The HW implementation of the Ruby take.
465
+ def stake(n)
466
+ return self.seach.stake(n)
467
+ end
468
+
469
+ # The HW implementation of the Ruby take_while.
470
+ def stake_while(&ruby_block)
471
+ return self.seach.stake_while(&ruby_block)
472
+ end
473
+
474
+ # HW implementation of the Ruby tally.
475
+ # NOTE: to do, or may be not.
476
+ def stally(h = nil)
477
+ raise "stally is not supported yet."
478
+ end
479
+
480
+ # HW implementation of the Ruby to_h.
481
+ # NOTE: to do, or may be not.
482
+ def sto_h(h = nil)
483
+ raise "sto_h is not supported yet."
484
+ end
485
+
486
+ # HW implementation of the Ruby uniq.
487
+ def suniq(&ruby_block)
488
+ return self.seach.suniq(&ruby_block)
489
+ end
490
+
491
+ # HW implementation of the Ruby zip.
492
+ # NOTE: for now szip is deactivated untile tuples are properly
493
+ # handled by HDLRuby.
494
+ def szip(obj,&ruby_block)
495
+ return self.seach.szip(obj,&ruby_block)
496
+ end
497
+
498
+ # Iterator on the +num+ next elements.
499
+ # *NOTE*:
500
+ # - Stop iteration when the end of the range is reached or when there
501
+ # are no elements left
502
+ # - This is not a method from Ruby but one specific for hardware where
503
+ # creating a array is very expensive.
504
+ def seach_nexts(num,&ruby_block)
505
+ return self.seach.snexts(num,&ruby_block)
506
+ end
507
+
508
+ end
509
+
510
+
511
+ # Modify String to act as Ruby code generator.
512
+ refine ::String do
513
+ # Convert to Ruby code.
514
+ def to_ruby
515
+ self
516
+ end
517
+
518
+ # Convert to C code.
519
+ alias_method :to_c, :to_ruby
520
+ end
521
+
522
+
523
+ # Modify Integer to act as value.
524
+ refine ::Integer do
525
+ def to_value
526
+ return Value.new(signed[32],self)
527
+ end
528
+ alias_method :to_expr, :to_value
529
+
530
+ def to_ruby
531
+ return self
532
+ end
533
+
534
+ def to_c
535
+ return self.to_s
536
+ end
537
+
538
+ # Enhance the Integer class with sequencer iterations.
539
+
540
+ # HW times iteration.
541
+ def stimes(&ruby_block)
542
+ self.to_value.stimes(&ruby_block)
543
+ end
544
+
545
+ # HW upto iteration.
546
+ def supto(val,&ruby_block)
547
+ self.to_value.supto(&ruby_block)
548
+ end
549
+
550
+ # HW downto iteration.
551
+ def sdownto(val,&ruby_block)
552
+ self.to_value.sdownto(&ruby_block)
553
+ end
554
+ end
555
+
556
+ # Modify Float to act as value.
557
+ refine ::Float do
558
+ def to_value
559
+ return Value.new(float[64],self)
560
+ end
561
+ alias_method :to_expr, :to_value
562
+ end
563
+
564
+ # Modify Range to act as RubyHDL object.
565
+ refine ::Range do
566
+ def to_ruby
567
+ return "(#{self})"
568
+ end
569
+ end
570
+
571
+ # Modify Range to support HW iterators.
572
+ refine ::Enumerable do
573
+ import_methods SEnumerable
574
+ # HW iteration on each element.
575
+ def seach(&ruby_block)
576
+ return Siter.new(RubyHDL::High.top_sblock.sequencer,self,"each",&ruby_block)
577
+ end
578
+ end
579
+
580
+
581
+
582
+
583
+ # Modify Array to build types.
584
+ refine ::Array do
585
+
586
+ # Convert to a type.
587
+ def to_type
588
+ # Compute the base type.
589
+ if self[0].is_a?(::Array) then
590
+ # Recurse build the base.
591
+ base = self[0].to_type
592
+ else
593
+ base = unsigned
594
+ end
595
+ # Generate the resulting type vector.
596
+ return TypeVector.new(:"",base,self[0])
597
+ end
598
+
599
+ # Generate signals from +names+.
600
+ def inner(*names)
601
+ # Generate the type.
602
+ type = self.to_type
603
+ # Generate the resulting signals.
604
+ return type.inner(*names)
605
+ end
606
+ end
607
+
608
+
609
+
610
+ # Describes a SW implementation of types.
611
+
612
+ ##
613
+ # Describes a high-level data type.
614
+ #
615
+ # NOTE: by default a type is not specified.
616
+ class Type
617
+ using RubyHDL::High
618
+ include HDLRuby::Tprocess
619
+
620
+ # Type creation.
621
+
622
+ # Creates a new type named +name+.
623
+ def initialize(name = nil)
624
+ if name then
625
+ @name = name.to_sym
626
+ # Registers the name.
627
+ self.register(name)
628
+ end
629
+ end
630
+
631
+ # Type access
632
+
633
+ # Comparison for hash: structural comparison.
634
+ def eql?(obj)
635
+ return false unless obj.is_a?(Type)
636
+ return false unless @name.eql?(obj.name)
637
+ return true
638
+ end
639
+
640
+ # Hash function.
641
+ def hash
642
+ return [@name].hash
643
+ end
644
+
645
+ # Tells if the type signed.
646
+ def signed?
647
+ return false
648
+ end
649
+
650
+ # Tells if the type is unsigned.
651
+ def unsigned?
652
+ return false
653
+ end
654
+
655
+ # Tells if the type is fixed point.
656
+ def fixed?
657
+ return false
658
+ end
659
+
660
+ # Tells if the type is floating point.
661
+ def float?
662
+ return false
663
+ end
664
+
665
+ # Tells if the type is a leaf.
666
+ def leaf?
667
+ return false
668
+ end
669
+
670
+ # Tells if the type of of vector kind.
671
+ def vector?
672
+ return false
673
+ end
674
+
675
+ # Gets the bitwidth of the type, by default 0.
676
+ # Bit, signed, unsigned and Float base have a width of 1.
677
+ def width
678
+ if [:bit, :signed, :unsigned, :float ].include?(@name) then
679
+ return 1
680
+ else
681
+ return 0
682
+ end
683
+ end
684
+
685
+ # Gets the type max value if any.
686
+ # Default: not defined.
687
+ def max
688
+ raise AnyError, "No max value for type #{self}"
689
+ end
690
+
691
+ # Gets the type min value if any.
692
+ # Default: not defined.
693
+ def min
694
+ raise AnyError, "No min value for type #{self}"
695
+ end
696
+
697
+ # Get the direction of the type, little or big endian.
698
+ def direction
699
+ # By default, little endian.
700
+ return :little
701
+ end
702
+
703
+ # Tells if the type has a range.
704
+ def range?
705
+ return false
706
+ end
707
+
708
+ # Gets the range of the type, by default range is not defined.
709
+ def range
710
+ raise AnyError, "No range for type #{self}"
711
+ end
712
+
713
+ # Tells if the type has a base.
714
+ def base?
715
+ return false
716
+ end
717
+
718
+ # Gets the base type, by default base type is not defined.
719
+ def base
720
+ raise AnyError, "No base type for type #{self}"
721
+ end
722
+
723
+ # Tells if the type has sub types.
724
+ def types?
725
+ return false
726
+ end
727
+
728
+ # Tells if the type is regular (applies for tuples).
729
+ def regular?
730
+ return false
731
+ end
732
+
733
+ # Tells if the type has named sub types.
734
+ def struct?
735
+ return false
736
+ end
737
+
738
+ # Tells if the type is hierarchical.
739
+ def hierarchical?
740
+ return self.base? || self.types?
741
+ end
742
+
743
+ # Tell if +type+ is equivalent to current type.
744
+ #
745
+ # NOTE: type can be compatible while not being equivalent, please
746
+ # refer to `hruby_types.rb` for type compatibility.
747
+ def equivalent?(type)
748
+ # By default, types are equivalent iff they have the same name.
749
+ return (type.is_a?(Type) and self.name == type.name)
750
+ end
751
+
752
+ # Iterates over the types deeply if any.
753
+ def each_type_deep(&ruby_block)
754
+ # No ruby block? Return an enumerator.
755
+ return to_enum(:each_type_deep) unless ruby_block
756
+ # A ruby block? First apply it to current.
757
+ ruby_block.call(self)
758
+ # And that's all by default.
759
+ end
760
+ alias_method :each_deep, :each_type_deep
761
+
762
+ # Converts to a bit vector.
763
+ def to_vector
764
+ return TypeVector.new(:"", Bit, self.width-1..0)
765
+ end
766
+
767
+ # Tells htype has been included.
768
+ def htype?
769
+ return true
770
+ end
771
+
772
+ # Converts to a type.
773
+ # Returns self since it is already a type.
774
+ def to_type
775
+ return self
776
+ end
777
+
778
+ # Sets the +name+.
779
+ #
780
+ # NOTE: can only be done if the name is not already set.
781
+ def name=(name)
782
+ unless @name.empty? then
783
+ raise AnyError, "Name of type already set to: #{@name}."
784
+ end
785
+ # Checks and sets the name.
786
+ name = name.to_sym
787
+ if name.empty? then
788
+ raise AnyError, "Cannot set an empty name."
789
+ end
790
+ @name = name
791
+ # Registers the name.
792
+ self.register(name)
793
+ end
794
+
795
+ # Register the +name+ of the type.
796
+ def register(name)
797
+ # Sets the hdl-like access to the type.
798
+ obj = self # For using the right self within the proc
799
+ RubyHDL::High.top_sblock.register(name) { obj }
800
+ end
801
+
802
+ # Gets the type as left value.
803
+ #
804
+ # NOTE: used for asymetric types like TypeSystemI.
805
+ def left
806
+ # By default self.
807
+ self
808
+ end
809
+
810
+ # Gets the type as right value.
811
+ #
812
+ # NOTE: used for asymetric types like TypeSystemI.
813
+ def right
814
+ # By default self.
815
+ self
816
+ end
817
+
818
+ # Type creation.
819
+
820
+ # Declares a new type definition with +name+ equivalent to current one.
821
+ def typedef(name)
822
+ # Create the new type.
823
+ typ = TypeDef.new(name,self)
824
+ # Register it.
825
+ High.top_sblock.register(name) { typ }
826
+ # Return it.
827
+ return typ
828
+ end
829
+
830
+ # Creates a new vector type of range +rng+ and with current type as
831
+ # base.
832
+ def [](rng)
833
+ return TypeVector.new(:"",self,rng)
834
+ end
835
+
836
+ # SignalI creation through the type.
837
+
838
+ # # Declares high-level input signals named +names+ of the current type.
839
+ # def input(*names)
840
+ # High.top_sblock.make_inputs(self,*names)
841
+ # end
842
+
843
+ # # Declares high-level untyped output signals named +names+ of the
844
+ # # current type.
845
+ # def output(*names)
846
+ # # High.top_user.make_outputs(self.instantiate,*names)
847
+ # High.top_sblock.make_outputs(self,*names)
848
+ # end
849
+
850
+ # # Declares high-level untyped inout signals named +names+ of the
851
+ # # current type.
852
+ # def inout(*names)
853
+ # # High.top_user.make_inouts(self.instantiate,*names)
854
+ # High.top_sblock.make_inouts(self,*names)
855
+ # end
856
+
857
+ # Declares high-level untyped inner signals named +names+ of the
858
+ # current type.
859
+ def inner(*names)
860
+ RubyHDL::High.top_sblock.make_inners(self,*names)
861
+ end
862
+
863
+ # Declares high-level untyped constant signals by name and
864
+ # value given by +hsh+ of the current type.
865
+ def constant(hsh)
866
+ RubyHDL::High.top_sblock.make_constants(self,hsh)
867
+ end
868
+
869
+ # Computations of expressions
870
+
871
+ # Gets the computation method for +operator+.
872
+ def comp_operator(op)
873
+ return (op.to_s + ":C").to_sym
874
+ end
875
+
876
+ # Performs unary operation +operator+ on expression +expr+.
877
+ def unary(operator,expr)
878
+ # Look for a specific computation method.
879
+ comp = comp_operator(operator)
880
+ if self.respond_to?(comp) then
881
+ # Found, use it.
882
+ self.send(comp,expr)
883
+ else
884
+ # Not found, back to default computation.
885
+ expr.to_value.send(operator)
886
+ end
887
+ end
888
+
889
+ # Performs binary operation +operator+ on expressions +expr0+
890
+ # and +expr1+.
891
+ def binary(operator, expr0, expr1)
892
+ # Look for a specific computation method.
893
+ comp = comp_operator(operator)
894
+ if self.respond_to?(comp) then
895
+ # Found, use it.
896
+ self.send(comp,expr0,expr1)
897
+ else
898
+ # Not found, back to default computation.
899
+ expr0.to_value.send(operator,expr1)
900
+ end
901
+ end
902
+
903
+ # Redefinition of +operator+.
904
+ def define_operator(operator,&ruby_block)
905
+ # Ensure there is a block.
906
+ ruby_block = proc {} unless block_given?
907
+ # Register the operator as overloaded.
908
+ @overloads ||= {}
909
+ @overloads[operator] = ruby_block
910
+ # Set the new method for the operator.
911
+ self.define_singleton_method(comp_operator(operator)) do |*args|
912
+ # puts "Top user=#{HDLRuby::High.top_user}"
913
+ RubyHDL::High.top_sblock.sub(RubyHDL.uniq_name) do
914
+ ruby_block.call(*args)
915
+ end
916
+ end
917
+ end
918
+
919
+ # Redefinition of +operator+ when requiring the context to be passed
920
+ # as argument (normally only used internally).
921
+ def define_operator_with_context(operator,&ruby_block)
922
+ # Ensure there is a block.
923
+ ruby_block = proc {} unless block_given?
924
+ # Register the operator as overloaded.
925
+ @overloads ||= {}
926
+ @overloads[operator] = ruby_block
927
+ # Set the new method for the operator.
928
+ self.define_singleton_method(comp_operator(operator)) do |*args|
929
+ # puts "Top user=#{HDLRuby::High.top_user}"
930
+ RubyHDL::High.top_sblock.sub(RubyHDL.uniq_name) do
931
+ # It is assumed that the first argument of the ruby_block
932
+ # is the context in which it must be executed.
933
+ ruby_block.call(self,*args)
934
+ end
935
+ end
936
+ end
937
+
938
+ # Interates over the overloaded operators.
939
+ def each_overload(&ruby_block)
940
+ # No ruby block? Return an enumerator.
941
+ return to_enum(:each_overload) unless ruby_block
942
+ # A block? Apply it on each overload if any.
943
+ @overloads.each(&ruby_block) if @overloads
944
+ end
945
+
946
+ # Convert to C code.
947
+ def to_c
948
+ case @name
949
+ when :void
950
+ return "void"
951
+ when :bit, :unsigned
952
+ return "unsigned"
953
+ when :signed
954
+ return "signed"
955
+ when :float
956
+ return "double"
957
+ when :string
958
+ return "char*"
959
+ else
960
+ return @name.to_s
961
+ end
962
+ end
963
+
964
+ # Convert to C initialization code.
965
+ def to_c_init
966
+ # By default: 0
967
+ return "0"
968
+ end
969
+ end
970
+
971
+
972
+ # Defines a basic type +name+.
973
+ def self.define_type(name)
974
+ name = name.to_sym
975
+ type = Type.new(name)
976
+ self.send(:define_method,name) { type }
977
+ return type
978
+ end
979
+
980
+ # The void type
981
+ Void = define_type(:void)
982
+
983
+ # The bit type.
984
+ Bit = define_type(:bit)
985
+
986
+ # The signed bit type.
987
+ Signed = define_type(:signed)
988
+
989
+ # The unsigned bit type.
990
+ Unsigned = define_type(:unsigned)
991
+
992
+ # The float bit type
993
+ Float = define_type(:float)
994
+
995
+ # The string type
996
+ StringT = define_type(:string)
997
+
998
+
999
+ ##
1000
+ # Describes a high-level type definition.
1001
+ #
1002
+ # NOTE: type definition are actually type with a name refering to another
1003
+ # type (and equivalent to it).
1004
+ class TypeDef < Type
1005
+ # Type creation.
1006
+
1007
+ # Creates a new type definition named +name+ refering +type+.
1008
+ def initialize(name,type)
1009
+ # Initialize the type structure.
1010
+ super(name)
1011
+ # Set the referenced type.
1012
+ @type = type.to_type
1013
+ end
1014
+ end
1015
+
1016
+ ##
1017
+ # Describes a high-level generic type definition.
1018
+ #
1019
+ # NOTE: this type does not correspond to any low-level type
1020
+ class TypeGen < Type
1021
+ # Type creation.
1022
+
1023
+ # Creates a new generic type definition producing a new type by
1024
+ # executing +ruby_block+.
1025
+ def initialize(name,&ruby_block)
1026
+ # Initialize the type structure.
1027
+ super(name)
1028
+ # Sets the block to execute when instantiating the type.
1029
+ @instance_proc = ruby_block
1030
+ end
1031
+
1032
+ # Generates the type with +args+ generic parameters.
1033
+ def generate(*args)
1034
+ # Generate the resulting type.
1035
+ gtype = RubyHDL::High.top_sblock.instance_exec(*args,&@instance_proc)
1036
+ # Ensures a type has been produced.
1037
+ gtype = gtype.to_type if gtype.respond_to?(:to_type)
1038
+ unless gtype.is_a?(Type) then
1039
+ raise AnyError, "Generic type #{self.name} did not produce a valid type: #{gtype.class}"
1040
+ end
1041
+ # Create a new type definition from it.
1042
+ gtype = TypeDef.new(self.name.to_s + "_#{args.join(':')}",
1043
+ gtype)
1044
+ # Adds the possible overloaded operators.
1045
+ self.each_overload do |op,ruby_block|
1046
+ gtype.define_operator_with_context(op,&(RubyHDL::High.curry_with_context(*args,&ruby_block)))
1047
+ end
1048
+ # Returns the resulting type
1049
+ return gtype
1050
+ end
1051
+ end
1052
+
1053
+
1054
+ ##
1055
+ # Describes a vector type.
1056
+ class TypeVector < Type
1057
+ # The base type of the vector
1058
+ attr_reader :base
1059
+
1060
+ # Tells if the type of of vector kind.
1061
+ def vector?
1062
+ return true
1063
+ end
1064
+
1065
+ # Tells if the type has a base.
1066
+ def base?
1067
+ return true
1068
+ end
1069
+
1070
+ # The range of the vector.
1071
+ attr_reader :range
1072
+
1073
+ # Creates a new vector type named +name+ from +base+ type and with
1074
+ # +range+.
1075
+ # NOTE: if +range+ is a positive integer it is converted to
1076
+ # (range-1)..0, if it is a negative integer it is converted to
1077
+ # 0..(-range-1)
1078
+ def initialize(name,base,range)
1079
+ # Initialize the type.
1080
+ super(name)
1081
+
1082
+ # Check and set the base
1083
+ unless base.is_a?(Type)
1084
+ raise AnyError,
1085
+ "Invalid class for VectorType base: #{base.class}."
1086
+ end
1087
+ @base = base
1088
+
1089
+ # Check and set the range.
1090
+ if range.respond_to?(:to_i) then
1091
+ # Integer case: convert to 0..(range-1).
1092
+ range = range > 0 ? (range-1)..0 : 0..(-range-1)
1093
+ elsif
1094
+ # Other cases: assume there is a first and a last to create
1095
+ # the range.
1096
+ range = range.first..range.last
1097
+ end
1098
+ @range = range
1099
+ end
1100
+
1101
+ # Comparison for hash: structural comparison.
1102
+ def eql?(obj)
1103
+ # # General type comparison.
1104
+ # return false unless super(obj)
1105
+ # Specific comparison.
1106
+ return false unless obj.is_a?(TypeVector)
1107
+ return false unless @base.eql?(obj.base)
1108
+ return false unless @range.eql?(obj.range)
1109
+ return true
1110
+ end
1111
+
1112
+ # Hash function.
1113
+ def hash
1114
+ return [super,@base,@range].hash
1115
+ end
1116
+
1117
+ # Gets the size of the type in number of base elements.
1118
+ def size
1119
+ return (@range.first.to_i - @range.last.to_i).abs + 1
1120
+ end
1121
+
1122
+ # Gets the bitwidth of the type, nil for undefined.
1123
+ #
1124
+ # NOTE: must be redefined for specific types.
1125
+ def width
1126
+ first = @range.first.to_i
1127
+ last = @range.last.to_i
1128
+ return @base.width * ((first-last).abs + 1)
1129
+ end
1130
+
1131
+ # Gets the type max value if any.
1132
+ def max
1133
+ if (self.signed?) then
1134
+ return (2**(self.width-1))-1
1135
+ else
1136
+ return (2**(self.width))-1
1137
+ end
1138
+ end
1139
+
1140
+ # Gets the type min value if any.
1141
+ # Default: not defined.
1142
+ def min
1143
+ if (self.signed?) then
1144
+ return -(2**(self.width-1))
1145
+ else
1146
+ return 0
1147
+ end
1148
+ end
1149
+
1150
+ # Get the direction of the type, little or big endian.
1151
+ def direction
1152
+ return @range.first < @range.last ? :big : :little
1153
+ end
1154
+
1155
+ # Gets the direction of the range.
1156
+ def dir
1157
+ return (@range.last - @range.first)
1158
+ end
1159
+
1160
+ # Tells if the type signed.
1161
+ def signed?
1162
+ return @base.signed?
1163
+ end
1164
+
1165
+ # Tells if the type is unsigned.
1166
+ def unsigned?
1167
+ return @base.unsigned?
1168
+ end
1169
+
1170
+ # Tells if the type is fixed point.
1171
+ def fixed?
1172
+ return @base.signed?
1173
+ end
1174
+
1175
+ # Tells if the type is floating point.
1176
+ def float?
1177
+ return @base.float?
1178
+ end
1179
+
1180
+ # Tell if +type+ is equivalent to current type.
1181
+ #
1182
+ # NOTE: type can be compatible while not being equivalent, please
1183
+ # refer to `hruby_types.rb` for type compatibility.
1184
+ def equivalent?(type)
1185
+ return (type.is_a?(TypeVector) and
1186
+ @range == type.range
1187
+ @base.equivalent?(type.base) )
1188
+ end
1189
+
1190
+ # Should not exists since it identifies types with multiple sub types.
1191
+ #
1192
+ # # Iterates over the sub types.
1193
+ # def each_type(&ruby_block)
1194
+ # # No ruby block? Return an enumerator.
1195
+ # return to_enum(:each_type) unless ruby_block
1196
+ # # A ruby block? Apply it on the base.
1197
+ # ruby_block.call(@base)
1198
+ # end
1199
+
1200
+ # Iterates over the types deeply if any.
1201
+ def each_type_deep(&ruby_block)
1202
+ # No ruby block? Return an enumerator.
1203
+ return to_enum(:each_type_deep) unless ruby_block
1204
+ # A ruby block? First apply it to current.
1205
+ ruby_block.call(self)
1206
+ # And recurse on the base.
1207
+ @base.each_type_deep(&ruby_block)
1208
+ end
1209
+
1210
+ alias_method :each_deep, :each_type_deep
1211
+
1212
+ # Convert to C code.
1213
+ def to_c
1214
+ if @base.is_a?(TypeVector) then
1215
+ # Array type case.
1216
+ return @base.to_c + "[#{self.size.to_i}]"
1217
+ else
1218
+ # Simple vector type case.
1219
+ if float? then
1220
+ return @base.to_c
1221
+ else
1222
+ return @base + " long long"
1223
+ end
1224
+ end
1225
+ end
1226
+
1227
+ # Convert to C initialization code.
1228
+ def to_c_init
1229
+ if @base.is_a?(TypeVector) then
1230
+ # Array type case.
1231
+ base_init = @base.to_c_init
1232
+ return "[" + ([base_init] * self.size.to_i).join(",") + "]"
1233
+ else
1234
+ return "0"
1235
+ end
1236
+ end
1237
+ end
1238
+
1239
+
1240
+ ##
1241
+ # Describes a signed integer data type.
1242
+ class TypeSigned < TypeVector
1243
+
1244
+ # Creates a new vector type named +name+ from +base+ type and with
1245
+ # +range+.
1246
+ #
1247
+ # NOTE:
1248
+ # * The default range is 32-bit.
1249
+ def initialize(name,range = 31..0)
1250
+ # Initialize the type.
1251
+ super(name,Signed,range)
1252
+ end
1253
+ end
1254
+
1255
+ ##
1256
+ # Describes an unsigned integer data type.
1257
+ class TypeUnsigned < TypeVector
1258
+
1259
+ # Creates a new vector type named +name+ from +base+ type and with
1260
+ # +range+.
1261
+ #
1262
+ # NOTE:
1263
+ # * The default range is 32-bit.
1264
+ def initialize(name,range = 31..0)
1265
+ # Initialize the type.
1266
+ super(name,Unsigned,range)
1267
+ end
1268
+ end
1269
+
1270
+ ##
1271
+ # Describes a float data type.
1272
+ class TypeFloat < TypeVector
1273
+
1274
+ # Creates a new vector type named +name+ from +base+ type and with
1275
+ # +range+.
1276
+ #
1277
+ # NOTE:
1278
+ # * The bits of negative range stands for the exponent
1279
+ # * The default range is for 64-bit IEEE 754 double precision standart
1280
+ def initialize(name,range = 52..-11)
1281
+ # Initialize the type.
1282
+ super(name,Float,range)
1283
+ end
1284
+ end
1285
+
1286
+ ##
1287
+ # Describes a tuple type.
1288
+ class TypeTuple < Type
1289
+ # Creates a new tuple type named +name+ width +direction+ and whose
1290
+ # sub types are given by +content+.
1291
+ def initialize(name,direction,*content)
1292
+ # Initialize the type.
1293
+ super(name)
1294
+
1295
+ # Set the direction.
1296
+ @direction = direction.to_sym
1297
+ unless [:little, :big].include?(@direction)
1298
+ raise AnyError, "Invalid direction for a type: #{direction}"
1299
+ end
1300
+
1301
+ # Check and set the content.
1302
+ content.each do |sub|
1303
+ unless sub.is_a?(Type) then
1304
+ raise AnyError, "Invalid class for a type: #{sub.class}"
1305
+ end
1306
+ end
1307
+ @types = content
1308
+ end
1309
+
1310
+ # Comparison for hash: structural comparison.
1311
+ def eql?(obj)
1312
+ # # General type comparison.
1313
+ # return false unless super(obj)
1314
+ return false unless obj.is_a?(TypeTuple)
1315
+ # Specific comparison.
1316
+ idx = 0
1317
+ obj.each_type do |type|
1318
+ return false unless @types[idx].eql?(type)
1319
+ idx += 1
1320
+ end
1321
+ return false unless idx == @types.size
1322
+ return true
1323
+ end
1324
+
1325
+ # Hash function.
1326
+ def hash
1327
+ return [super,@types].hash
1328
+ end
1329
+
1330
+ # Tells if the type has sub types.
1331
+ def types?
1332
+ return true
1333
+ end
1334
+
1335
+ # Gets an array containing all the syb types.
1336
+ def get_all_types
1337
+ return @types.clone
1338
+ end
1339
+
1340
+ # Gets a sub type by +index+.
1341
+ def get_type(index)
1342
+ return @types[index.to_i]
1343
+ end
1344
+
1345
+ # Adds a sub +type+.
1346
+ def add_type(type)
1347
+ unless type.is_a?(Type) then
1348
+ raise AnyError,
1349
+ "Invalid class for a type: #{type.class} (#{type})"
1350
+ end
1351
+ @types << type
1352
+ end
1353
+
1354
+ # Iterates over the sub name/type pair.
1355
+ #
1356
+ # Returns an enumerator if no ruby block is given.
1357
+ def each(&ruby_block)
1358
+ # No ruby block? Return an enumerator.
1359
+ return to_enum(:each) unless ruby_block
1360
+ # A ruby block? Apply it on each sub name/type pair.
1361
+ @types.each(&ruby_block)
1362
+ end
1363
+
1364
+ # Iterates over the sub types.
1365
+ #
1366
+ # Returns an enumerator if no ruby block is given.
1367
+ def each_type(&ruby_block)
1368
+ # No ruby block? Return an enumerator.
1369
+ return to_enum(:each_type) unless ruby_block
1370
+ # A ruby block? Apply it on each sub type.
1371
+ @types.each(&ruby_block)
1372
+ end
1373
+
1374
+ # Iterates over the types deeply if any.
1375
+ def each_type_deep(&ruby_block)
1376
+ # No ruby block? Return an enumerator.
1377
+ return to_enum(:each_type_deep) unless ruby_block
1378
+ # A ruby block? First apply it to current.
1379
+ ruby_block.call(self)
1380
+ # And recurse on the sub types.
1381
+ @types.each { |type| type.each_type_deep(&ruby_block) }
1382
+ end
1383
+
1384
+ alias_method :each_deep, :each_type_deep
1385
+
1386
+ # Tell if the tuple is regular, i.e., all its sub types are equivalent.
1387
+ #
1388
+ # NOTE: empty tuples are assumed not to be regular.
1389
+ def regular?
1390
+ return false if @types.empty?
1391
+ t0 = @types[0]
1392
+ @types[1..-1].each do |type|
1393
+ return false unless t0.equivalent?(type)
1394
+ end
1395
+ return true
1396
+ end
1397
+
1398
+ # Gets the bitwidth.
1399
+ def width
1400
+ return @types.reduce(0) { |sum,type| sum + type.width }
1401
+ end
1402
+
1403
+ # Get the direction of the type, little or big endian.
1404
+ def direction
1405
+ return @direction
1406
+ end
1407
+
1408
+ # Gets the range of the type.
1409
+ #
1410
+ # NOTE: only valid if the tuple is regular (i.e., all its sub types
1411
+ # are identical)
1412
+ def range
1413
+ if regular? then
1414
+ # Regular tuple, return its range as if it was an array.
1415
+ return 0..@types.size-1
1416
+ else
1417
+ raise AnyError, "No range for type #{self}"
1418
+ end
1419
+ end
1420
+
1421
+ # Tells if the type has a base.
1422
+ #
1423
+ # NOTE: only if the tuple is regular (i.e., all its sub types
1424
+ # are identical)
1425
+ def base?
1426
+ return regular?
1427
+ end
1428
+
1429
+ # Gets the base type.
1430
+ #
1431
+ # NOTE: only valid if the tuple is regular (i.e., all its sub types
1432
+ # are identical)
1433
+ def base
1434
+ if regular? then
1435
+ # Regular tuple, return the type of its first element.
1436
+ return @types[0]
1437
+ else
1438
+ raise AnyError, "No base type for type #{self}"
1439
+ end
1440
+ end
1441
+
1442
+ # Tell if +type+ is equivalent to current type.
1443
+ #
1444
+ # NOTE: type can be compatible while not being equivalent, please
1445
+ # refer to `hruby_types.rb` for type compatibility.
1446
+ def equivalent?(type)
1447
+ return (type.is_a?(TypeTuple) and
1448
+ !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) })
1449
+ end
1450
+ end
1451
+
1452
+ ##
1453
+ # Describes a struct type.
1454
+ class TypeStruct < Type
1455
+ attr_reader :direction
1456
+
1457
+ # Creates a new structure type named +name+ with direction +dir+ and
1458
+ # whose hierachy is given by +content+.
1459
+ def initialize(name,dir,content)
1460
+ # Initialize the type.
1461
+ super(name)
1462
+
1463
+ # Set the direction.
1464
+ @direction = dir.to_sym
1465
+ unless [:little, :big].include?(@direction)
1466
+ raise AnyError, "Invalid direction for a type: #{dir}"
1467
+ end
1468
+
1469
+ # Check and set the content.
1470
+ content = Hash[content]
1471
+ @types = content.map do |k,v|
1472
+ unless v.is_a?(Type) then
1473
+ raise AnyError, "Invalid class for a type: #{v.class}"
1474
+ end
1475
+ [ k.to_sym, v ]
1476
+ end.to_h
1477
+ end
1478
+
1479
+ # Comparison for hash: structural comparison.
1480
+ def eql?(obj)
1481
+ # General type comparison.
1482
+ # return false unless super(obj)
1483
+ return false unless obj.is_a?(TypeStruct)
1484
+ # Specific comparison.
1485
+ idx = 0
1486
+ obj.each_key do |name|
1487
+ return false unless @types[name].eql?(obj.get_type(name))
1488
+ idx += 1
1489
+ end
1490
+ return false unless idx == @types.size
1491
+ return true
1492
+ end
1493
+
1494
+ # Hash function.
1495
+ def hash
1496
+ return [super,@types].hash
1497
+ end
1498
+
1499
+ # Tells if the type has named sub types.
1500
+ def struct?
1501
+ return true
1502
+ end
1503
+
1504
+ # Tells if the type has sub types.
1505
+ def types?
1506
+ return true
1507
+ end
1508
+
1509
+ # Gets an array containing all the syb types.
1510
+ def get_all_types
1511
+ return @types.values
1512
+ end
1513
+
1514
+ # Gets a sub type by +name+.
1515
+ # NOTE: +name+ can also be an index.
1516
+ def get_type(name)
1517
+ if name.respond_to?(:to_sym) then
1518
+ return @types[name.to_sym]
1519
+ else
1520
+ return @types.values[name.to_i]
1521
+ end
1522
+ end
1523
+
1524
+ # Iterates over the sub name/type pair.
1525
+ #
1526
+ # Returns an enumerator if no ruby block is given.
1527
+ def each(&ruby_block)
1528
+ # No ruby block? Return an enumerator.
1529
+ return to_enum(:each) unless ruby_block
1530
+ # A ruby block? Apply it on each sub name/type pair.
1531
+ @types.each(&ruby_block)
1532
+ end
1533
+
1534
+ # Iterates over the sub types.
1535
+ #
1536
+ # Returns an enumerator if no ruby block is given.
1537
+ def each_type(&ruby_block)
1538
+ # No ruby block? Return an enumerator.
1539
+ return to_enum(:each_type) unless ruby_block
1540
+ # A ruby block? Apply it on each sub type.
1541
+ @types.each_value(&ruby_block)
1542
+ end
1543
+
1544
+ # Iterates over the keys.
1545
+ #
1546
+ # Returns an enumerator if no ruby block is given.
1547
+ def each_key(&ruby_block)
1548
+ # No ruby block? Return an enumerator.
1549
+ return to_enum(:each_key) unless ruby_block
1550
+ # A ruby block? Apply it on each key.
1551
+ @types.each_key(&ruby_block)
1552
+ end
1553
+
1554
+ # Iterates over the sub type names.
1555
+ #
1556
+ # Returns an enumerator if no ruby block is given.
1557
+ def each_name(&ruby_block)
1558
+ # No ruby block? Return an enumerator.
1559
+ return to_enum(:each_name) unless ruby_block
1560
+ # A ruby block? Apply it on each name.
1561
+ @types.each_key(&ruby_block)
1562
+ end
1563
+
1564
+ # Iterates over the types deeply if any.
1565
+ def each_type_deep(&ruby_block)
1566
+ # No ruby block? Return an enumerator.
1567
+ return to_enum(:each_type_deep) unless ruby_block
1568
+ # A ruby block? First apply it to current.
1569
+ ruby_block.call(self)
1570
+ # And recurse on the sub types.
1571
+ @types.each_value { |type| type.each_type_deep(&ruby_block) }
1572
+ end
1573
+
1574
+ alias_method :each_deep, :each_type_deep
1575
+
1576
+ # Gets the bitwidth of the type, nil for undefined.
1577
+ #
1578
+ # NOTE: must be redefined for specific types.
1579
+ def width
1580
+ if @types.is_a?(Array) then
1581
+ return @types.reduce(0) {|sum,type| sum + type.width }
1582
+ else
1583
+ return @types.each_value.reduce(0) {|sum,type| sum + type.width }
1584
+ end
1585
+ end
1586
+
1587
+ # Tell if +type+ is equivalent to current type.
1588
+ #
1589
+ # NOTE: type can be compatible while not being equivalent, please
1590
+ # refer to `hruby_types.rb` for type compatibility.
1591
+ def equivalent?(type)
1592
+ return (type.is_a?(TypeStruct) and
1593
+ !@types.to_a.zip(type.types.to_a).index do |t0,t1|
1594
+ t0[0] != t1[0] or !t0[1].equivalent?(t1[1])
1595
+ end)
1596
+ end
1597
+ end
1598
+
1599
+ # The type constructors.
1600
+
1601
+ # Creates an unnamed structure type from a +content+.
1602
+ def struct(content)
1603
+ return TypeStruct.new(:"",:little,content)
1604
+ end
1605
+
1606
+ # Methods for declaring types
1607
+
1608
+ # Declares a high-level generic type named +name+, and using +ruby_block+
1609
+ # for construction.
1610
+ def typedef(name, &ruby_block)
1611
+ # Ensure there is a block.
1612
+ ruby_block = proc {} unless block_given?
1613
+ type = TypeGen.new(name,&ruby_block)
1614
+ define_singleton_method(name.to_sym) do |*args|
1615
+ if (args.size < ruby_block.arity) then
1616
+ # Not enough arguments get generic type as is.
1617
+ type
1618
+ else
1619
+ # There are arguments, specialize the type.
1620
+ gtype = type.generate(*args)
1621
+ # And add it as a local type of the system.
1622
+ RubyHDL::High.top_sblock.add_type(gtype)
1623
+ gtype
1624
+ end
1625
+ end
1626
+ end
1627
+
1628
+
1629
+
1630
+ ## Describes the software implementation of an expression.
1631
+ class Expression
1632
+ using RubyHDL::High
1633
+
1634
+ attr_reader :type
1635
+ # Create a new expression with +type+ data type.
1636
+ def initialize(type)
1637
+ @type = type.to_type
1638
+ end
1639
+
1640
+ # Converts to an expression.
1641
+ def to_expr
1642
+ self
1643
+ end
1644
+
1645
+ alias_method :to_ref, :to_expr
1646
+
1647
+ # Compute the expression (convert it to a value).
1648
+ def to_value
1649
+ raise "to_value not defined here."
1650
+ end
1651
+
1652
+ # Convert to Ruby code.
1653
+ def to_ruby
1654
+ raise "to_ruby not defined for class: #{self.class}."
1655
+ end
1656
+
1657
+ # Convert to C code.
1658
+ def to_c
1659
+ raise "to_c not defined for class: #{self.class}."
1660
+ end
1661
+
1662
+ # Convert to ruby code for left value.
1663
+ # By default: the same as to_ruby
1664
+ alias_method :to_ruby_left, :to_ruby
1665
+
1666
+ # The implementation of the expressions generation.
1667
+
1668
+ # The unary operations generation.
1669
+ [:"-@",:"+@",:"~", :abs,
1670
+ :boolean, :bit, :signed, :unsigned].each do |operator|
1671
+ self.define_method(operator) do
1672
+ return Unary.new(self.type,operator,self.to_expr)
1673
+ end
1674
+ end
1675
+
1676
+ # The binary operations generation.
1677
+ [:"+",:"-",:"*",:"/",:"%",:"**",
1678
+ :"&",:"|",:"^",
1679
+ :"<<",:">>",# :ls,:rs,:lr,:rr, # ls, rs lr and rr are treated separately
1680
+ :"==",:"!=",:"<",:">",:">="].each do |operator|
1681
+ # :"<=" is processed appart for transmit.
1682
+ self.define_method(operator) do |right|
1683
+ return Binary.new(self.type,operator,self.to_expr,right.to_expr)
1684
+ end
1685
+ end
1686
+
1687
+ # The <= operator which can be either a transmit or a comparison.
1688
+ # By default set to transmit, and converted to comparison if
1689
+ # child of operator or condition of sif/swhile statements.
1690
+ def <=(right)
1691
+ return Transmit.new(self.to_expr,right.to_expr)
1692
+ end
1693
+
1694
+ # TO DO?
1695
+ # # The <=> operator is also supported but is transformed into a sub
1696
+ # # with a signed result.
1697
+ # def <=>(right)
1698
+ # return (self.as(self.type.base[self.type.width+1])-right).to_signed
1699
+ # end
1700
+
1701
+ # Creates an access to elements of range +rng+ of the signal,
1702
+ # and set the type of elements as +typ+ if given.
1703
+ #
1704
+ # NOTE: +rng+ can be a single expression in which case it is an index.
1705
+ def [](typ,rng=nil)
1706
+ # Treat the number of arguments
1707
+ rng, typ = typ, nil unless rng
1708
+ # Process the range.
1709
+ if rng.is_a?(::Range) then
1710
+ first = rng.first
1711
+ if (first.is_a?(::Integer)) then
1712
+ first = self.type.size+first if first < 0
1713
+ end
1714
+ last = rng.last
1715
+ if (last.is_a?(::Integer)) then
1716
+ last = self.type.size+last if last < 0
1717
+ end
1718
+ rng = first..last
1719
+ end
1720
+ if rng.is_a?(::Integer) && rng < 0 then
1721
+ rng = self.type.size+rng
1722
+ end
1723
+ if rng.respond_to?(:to_expr) then
1724
+ # Number range: convert it to an expression.
1725
+ rng = rng.to_expr
1726
+ end
1727
+ if rng.is_a?(Expression) then
1728
+ # Index case
1729
+ if typ then
1730
+ return RefIndex.new(typ,self.to_expr,rng)
1731
+ else
1732
+ return RefIndex.new(self.type.base,self.to_expr,rng)
1733
+ end
1734
+ else
1735
+ # Range case, ensure it is made among expression.
1736
+ first = rng.first.to_expr
1737
+ last = rng.last.to_expr
1738
+ # And create the reference.
1739
+ if typ then
1740
+ return RefRange.new(typ,
1741
+ self.to_expr,first..last)
1742
+ else
1743
+ return RefRange.new(self.type.slice(first..last),
1744
+ self.to_expr,first..last)
1745
+ end
1746
+ end
1747
+ end
1748
+
1749
+ # Converts to a select operator using current expression as
1750
+ # condition for one of the +choices+.
1751
+ #
1752
+ # NOTE: +choices+ can either be a list of arguments or an array.
1753
+ # If +choices+ has only two entries
1754
+ # (and it is not a hash), +value+ will be converted to a boolean.
1755
+ def mux(*choices)
1756
+ # Process the choices.
1757
+ choices = choices.flatten(1) if choices.size == 1
1758
+ choices.map! { |choice| choice.to_expr }
1759
+ # Generate the select expression.
1760
+ return Select.new(choices[0].type,self.to_expr,*choices)
1761
+ end
1762
+
1763
+ # The implementation of the interators.
1764
+
1765
+ # HW iteration on each element.
1766
+ def seach(&ruby_block)
1767
+ RubyHDL::High.top_sblock <<
1768
+ Siter.new(RubyHDL::High.top_sblock.sequencer,self,"each",&ruby_block)
1769
+ end
1770
+
1771
+ # HW times iteration.
1772
+ def stimes(&ruby_block)
1773
+ RubyHDL::High.top_sblock <<
1774
+ Siter.new(RubyHDL::High.top_sblock.sequencer,self,"times",&ruby_block)
1775
+ end
1776
+
1777
+ # HW upto iteration.
1778
+ def supto(val,&ruby_block)
1779
+ RubyHDL::High.top_sblock <<
1780
+ Siter.new(RubyHDL::High.top_sblock.sequencer,self,"upto",&ruby_block)
1781
+ end
1782
+
1783
+ # HW downto iteration.
1784
+ def sdownto(val,&ruby_block)
1785
+ RubyHDL::High.top_sblock <<
1786
+ Siter.new(RubyHDL::High.top_sblock.sequencer,self,"downto",&ruby_block)
1787
+ end
1788
+ end
1789
+
1790
+
1791
+ ##
1792
+ # Describes the software implementation of a value.
1793
+ class Value < Expression
1794
+
1795
+ attr_reader :content
1796
+
1797
+ # Create a new value with +type+ data type and content +content+.
1798
+ def initialize(type,content)
1799
+ super(type)
1800
+ @content = content
1801
+ end
1802
+
1803
+ # Compute the expression (convert it to a value).
1804
+ def to_value
1805
+ return self
1806
+ end
1807
+
1808
+ # Convert to Ruby code.
1809
+ def to_ruby
1810
+ return @content.to_s
1811
+ end
1812
+
1813
+ # Convert to C code.
1814
+ alias_method :to_c, :to_ruby
1815
+ end
1816
+
1817
+
1818
+ # Describes the software implementation of an unary operation.
1819
+ class Unary < Expression
1820
+ # Create a new unary operation with +type+ data type,
1821
+ # operator +operator+ and operand +child+
1822
+ def initialize(type,operator,child)
1823
+ super(type)
1824
+ @operator = operator.to_sym
1825
+ @child = child.to_expr
1826
+ end
1827
+
1828
+ # Convert to Ruby code.
1829
+ def to_ruby
1830
+ return RUBY_OPERATOR[@operator] % @child.to_ruby
1831
+ end
1832
+
1833
+ # Convert to C code.
1834
+ def to_c
1835
+ return C_OPERATOR[@operator] % @child.to_c
1836
+ end
1837
+ end
1838
+
1839
+ # Describes the software implementation of an binary operation.
1840
+ class Binary < Expression
1841
+ # Create a new binary operation with +type+ data type,
1842
+ # operator +operator+ and operands +left+ and +right+.
1843
+ def initialize(type,operator,left,right)
1844
+ super(type)
1845
+ @operator = operator.to_sym
1846
+ @left = left.to_expr
1847
+ @right = right.to_expr
1848
+ end
1849
+
1850
+ # Convert to Ruby code.
1851
+ def to_ruby
1852
+ return RUBY_OPERATOR[@operator] % [ @left.to_ruby, @right.to_ruby ]
1853
+ end
1854
+
1855
+ # Convert to C code.
1856
+ def to_c
1857
+ return C_OPERATOR[@operator] % [ @left.to_c, @right.to_c ]
1858
+ end
1859
+ end
1860
+
1861
+ # Describes the software implementation of an select operation.
1862
+ class Select < Expression
1863
+ # Create a new select operation with +type+ data type,
1864
+ # selection operand +sel+ and possible choices +choices+.
1865
+ def initialize(type,operator,sel,*choices)
1866
+ super(type)
1867
+ @sel = sel.to_expr
1868
+ @choices = choices.map(&:to_expr)
1869
+ end
1870
+
1871
+ # Convert to Ruby code.
1872
+ def to_ruby
1873
+ return "case(#{@sel.to_}) ; " +
1874
+ @choices.map.with_index do |choice,i|
1875
+ "when #{i} ; #{choice.to_ruby} ; "
1876
+ end.join + "end"
1877
+ end
1878
+
1879
+ # Convert to C code.
1880
+ def to_c
1881
+ return "switch(#{@sel.to_c}) {\n" +
1882
+ @choices.map.with_index do |choice,i|
1883
+ "case #{i}:\n#{choice.to_c}\nbreak;"
1884
+ end.join("\n") + "\n}"
1885
+ end
1886
+ end
1887
+
1888
+ # Describes a SW implementation of a reference.
1889
+ class Ref < Expression
1890
+ def to_ref
1891
+ return self
1892
+ end
1893
+
1894
+ # Get the final base: by default self.
1895
+ def final_base
1896
+ return self
1897
+ end
1898
+
1899
+ # # Get the final first index of the range, by default: 0
1900
+ # def final_first
1901
+ # return 0
1902
+ # end
1903
+
1904
+ # # Get the final range, by default: @type.width-1..0
1905
+ # def final_range
1906
+ # (@type.width-1)..0
1907
+ # end
1908
+ end
1909
+
1910
+ # Describes a SW implementation of a reference by name.
1911
+ class RefName < Ref
1912
+ # Create a new named reference with +type+ data type and +name+ name.
1913
+ def initialize(type,name)
1914
+ super(type)
1915
+ @name = name.to_sym
1916
+ end
1917
+
1918
+ # Convert to Ruby code.
1919
+ def to_ruby
1920
+ return @name.to_s
1921
+ end
1922
+
1923
+ # Convert to C code.
1924
+ alias_method :to_c, :to_ruby
1925
+ end
1926
+
1927
+ # Describes a SW implementation of an index reference.
1928
+ class RefIndex < Ref
1929
+ attr_reader :base
1930
+
1931
+ # Create a new index reference with +type+ data type +base+ base
1932
+ # reference and +idx+ index.
1933
+ def initialize(type,base,idx)
1934
+ super(type)
1935
+ @base = base.to_expr
1936
+ @idx = idx.to_expr
1937
+ end
1938
+
1939
+ # Get the final base object of the binary if it is an [] operator.
1940
+ def final_base
1941
+ if @base.is_a?(Ref) then
1942
+ return @base.final_base
1943
+ else
1944
+ return @base
1945
+ end
1946
+ end
1947
+
1948
+ # # Get the final first index.
1949
+ # def final_first
1950
+ # btyp = @base.type.base
1951
+ # idx = @base.type.dir <= 0 ? @idx : -@idx
1952
+ # # Compute the current access range.
1953
+ # first = idx*btyp.width
1954
+ # # Position it if there are upper ref access.
1955
+ # if @base.is_a?(Ref) then
1956
+ # first += @base.final_first
1957
+ # end
1958
+ # return first
1959
+ # end
1960
+
1961
+ # # Get the final access range.
1962
+ # def final_range
1963
+ # btyp = @base.type.base
1964
+ # idx = @base.type.dir <= 0 ? @idx : -@idx
1965
+ # # Compute the current access range.
1966
+ # rng = ((idx+1)*btyp.width-1)..(idx*btyp.width)
1967
+ # # Position it if there are upper ref access.
1968
+ # if @base.is_a?(Ref) then
1969
+ # rng.first += @base.final_first
1970
+ # rng.last += @base.final_first
1971
+ # end
1972
+ # return rng
1973
+ # end
1974
+
1975
+ # Get the access range.
1976
+ def range
1977
+ return @idx..@idx
1978
+ end
1979
+
1980
+ # Convert to Ruby code.
1981
+ def to_ruby
1982
+ return "#{@base.to_ruby}[#{@idx.to_ruby}]"
1983
+ end
1984
+
1985
+ # Convert to C code.
1986
+ def to_c
1987
+ return "#{@base.to_c}[#{@idx.to_c}]"
1988
+ end
1989
+ end
1990
+
1991
+ # Describes a SW implementation of an range reference.
1992
+ class RefRange < Ref
1993
+ attr_reader :base
1994
+
1995
+ # Create a new index reference with +type+ data type +base+ base
1996
+ # reference and +rng+ range.
1997
+ def initialize(type,base,rng)
1998
+ super(type)
1999
+ @base = base.to_ref
2000
+ @rng = (rng.first.to_expr)..(rng.last.to_expr)
2001
+ end
2002
+
2003
+ # Get the access range.
2004
+ def range
2005
+ return @rng.first..@rng.last
2006
+ end
2007
+
2008
+ # Get the final base object of the binary if it is an [] operator.
2009
+ def final_base
2010
+ if @base.is_a?(Ref) then
2011
+ return @base.final_base
2012
+ else
2013
+ return @base
2014
+ end
2015
+ end
2016
+
2017
+ # # Get the final first index.
2018
+ # def final_first
2019
+ # btyp = @base.type.base
2020
+ # idx = @base.type.dir <= 0 ? @rng.first : @rng.last
2021
+ # # Compute the current access range.
2022
+ # first = idx*btyp.width
2023
+ # # Position it if there are upper ref access.
2024
+ # if @base.is_a?(Ref) then
2025
+ # first += @base.final_first
2026
+ # end
2027
+ # return first
2028
+ # end
2029
+
2030
+ # # Get the final access range.
2031
+ # def final_range
2032
+ # btyp = @base.type.base
2033
+ # rng = @base.type.dir <= 0 ? @rng : @rng.last..@rng.first
2034
+ # # Compute the current access range.
2035
+ # rng = ((rng.first+1)*btyp.width-1)..(rng.last*btyp.width)
2036
+ # # Position it if there are upper ref access.
2037
+ # if @base.is_a?(Ref) then
2038
+ # rng.first += @base.final_first
2039
+ # rng.last += @base.final_first
2040
+ # end
2041
+ # return rng
2042
+ # end
2043
+
2044
+ # Convert to Ruby code.
2045
+ def to_ruby
2046
+ return "#{@base.to_ruby}[#{@rng.first.to_ruby}..#{@rng.last.to_ruby}]"
2047
+ end
2048
+
2049
+ # Convert to C code.
2050
+ def to_c
2051
+ if @base.type.base.is_a?(TypeVector) then
2052
+ raise "Range access not supported yet for arrays."
2053
+ else
2054
+ # Compute the writing and clearing masks
2055
+ smask = (1.to_value<<(@rng.first+1-@rng.last))-1
2056
+ cmask = ~(smask << @rng.last)
2057
+ # Get the final base.
2058
+ base = @base.final_base.to_c
2059
+ # Generate the ruby code.
2060
+ return "(#{base} & #{cmask.to_c}) >> (#{@rng.last.to_c})"
2061
+ end
2062
+ end
2063
+ end
2064
+
2065
+
2066
+
2067
+ # Describes a SW implementation of a transmit statement.
2068
+ class Transmit
2069
+ using RubyHDL::High
2070
+
2071
+ attr_reader :left, :right
2072
+
2073
+ # Create a new transmit statement with left value +left+ and
2074
+ # right value +right+.
2075
+ def initialize(left,right)
2076
+ @left = left.to_expr
2077
+ @right = right.to_expr
2078
+ # Add the transmit to the top SW block.
2079
+ # (It will be removed after if it was actually a comparison).
2080
+ RubyHDL::High.top_sblock << self
2081
+ end
2082
+
2083
+ # Convert to expression: transforms the transmit to a comparison.
2084
+ def to_expr
2085
+ # Remove the transmit from the top SW block.
2086
+ RubyHDL::High.top_sblock.delete(self)
2087
+ # And convert it to a comparison.
2088
+ return Binary.new(@left.type,@left,@right)
2089
+ end
2090
+
2091
+ # Convert to Ruby code.
2092
+ def to_ruby
2093
+ if (@left.is_a?(RefIndex) or @left.is_a?(RefRange)) then
2094
+ if @left.base.type.base.is_a?(TypeVector) then
2095
+ # Assign inside array.
2096
+ base = @left.final_base.to_ruby
2097
+ return "#{base} ||= []; #{@left.to_ruby} = #{@right.to_ruby}"
2098
+ else
2099
+ # Get the access range.
2100
+ rng = @left.range
2101
+ # Compute the writing and clearing masks
2102
+ smask = (1.to_value<<(rng.first+1-rng.last))-1
2103
+ cmask = ~(smask << rng.last)
2104
+ # Get the final base.
2105
+ base = left.final_base.to_ruby
2106
+ # Generate the ruby code.
2107
+ return "#{base} &= #{cmask.to_ruby}; " +
2108
+ "#{base} |= (((#{@right.to_ruby} & #{smask.to_ruby}) << (#{rng.last.to_ruby})))"
2109
+ end
2110
+ else
2111
+ return "#{@left.to_ruby} = #{@right.to_ruby}"
2112
+ end
2113
+ end
2114
+
2115
+ # Convert to C code.
2116
+ def to_c
2117
+ if (@left.is_a?(RefIndex) or @left.is_a?(RefRange)) then
2118
+ if @left.base.type.base.is_a?(TypeVector) then
2119
+ return "#{@left.to_c} = #{@right.to_c}"
2120
+ else
2121
+ # Get the access range.
2122
+ rng = @left.range
2123
+ # Compute the writing and clearing masks
2124
+ smask = (1.to_value<<(rng.first+1-rng.last))-1
2125
+ cmask = ~(smask << rng.last)
2126
+ # Get the final base.
2127
+ base = left.final_base.to_c
2128
+ # Generate the ruby code.
2129
+ return "#{base} &= #{cmask.to_c}; " +
2130
+ "#{base} |= (((#{@right.to_c} & #{smask.to_c}) << (#{rng.last.to_c})))"
2131
+ end
2132
+ else
2133
+ return "#{@left.to_c} = #{@right.to_c};"
2134
+ end
2135
+ end
2136
+
2137
+ end
2138
+
2139
+ # Describes a SW implementation of a if statement.
2140
+ class Sif
2141
+ # Create a new if statement in sequencer +sequencer+
2142
+ # with +cond+ condition and +ruby_block+
2143
+ # for generating the block that is taken if the condition is met.
2144
+ def initialize(sequencer,cond, &ruby_block)
2145
+ @sequencer = sequencer
2146
+ @condition = cond.to_expr
2147
+ @yes_blk = Sblock.new(@sequencer,&ruby_block)
2148
+ @elsifs = []
2149
+ @else_blk = nil
2150
+ end
2151
+
2152
+ # Add a selsif case.
2153
+ def selsif(cond,&ruby_block)
2154
+ @elsifs << [cond,Sblock.new(@sequencer,&ruby_block)]
2155
+ end
2156
+
2157
+ # Sets the else block.
2158
+ def selse(&ruby_block)
2159
+ @else_blk = Sblock.new(@sequencer,&ruby_block)
2160
+ end
2161
+
2162
+ # Convert to Ruby code.
2163
+ def to_ruby
2164
+ res = @sequencer.clk_up + "\nif(#{@condition.to_ruby})\n#{@yes_blk.to_ruby}\n"
2165
+ @elsifs.each do |(cond,blk)|
2166
+ res << "elsif(#{cond.to_ruby})\n#{blk.to_ruby}\n"
2167
+ end
2168
+ if @else_blk then
2169
+ res << "else\n#{@else_blk.to_ruby}\n"
2170
+ end
2171
+ return res + "end\n" + @sequencer.clk_up
2172
+ end
2173
+
2174
+ # Convert to C code.
2175
+ def to_c
2176
+ res = @sequencer.clk_up + "\nif(#{@condition.to_c}) {\n#{@yes_blk.to_c}\n}"
2177
+ @elsifs.each do |(cond,blk)|
2178
+ res << "\nelse if(#{cond.to_c}) {\n#{blk.to_c}\n}"
2179
+ end
2180
+ if @else_blk then
2181
+ res << "\nelse {\n#{@else_blk.to_c}\n}"
2182
+ end
2183
+ return res + @sequencer.clk_up_c
2184
+ end
2185
+ end
2186
+
2187
+ # Describes a SW implementation of a loop statement.
2188
+ class Sloop
2189
+ # Create a new infinite loop statement in sequencer +sequencer+
2190
+ # with +ruby_block+ for generating the block that is taken.
2191
+ def initialize(sequencer, &ruby_block)
2192
+ @sequencer = sequencer
2193
+ @blk = Sblock.new(sequencer,&ruby_block)
2194
+ end
2195
+
2196
+ # Convert to Ruby code.
2197
+ def to_ruby
2198
+ return "loop do\n#{@blk.to_ruby}\n#{@sequencer.clk_up}\nend"
2199
+ end
2200
+
2201
+ # Convert to C code.
2202
+ def to_c
2203
+ return "for(;;){\n#{@blk.to_ruby}\n#{@sequencer.clk_up_c}\n}"
2204
+ end
2205
+ end
2206
+
2207
+ # Describes a SW implementation of a while statement.
2208
+ class Swhile
2209
+ # Create a new while statement in sequencer +sequencer+
2210
+ # with +cond+ condition and +ruby_block+
2211
+ # for generating the block that is taken while the condition is met.
2212
+ def initialize(sequencer,cond, &ruby_block)
2213
+ @sequencer = sequencer
2214
+ @condition = cond.to_expr
2215
+ @yes_blk = Sblock.new(sequencer,&ruby_block)
2216
+ end
2217
+
2218
+ # Convert to Ruby code.
2219
+ def to_ruby
2220
+ return @sequencer.clk_up +
2221
+ "\nwhile(#{@condition.to_ruby}) do\n#{@yes_blk.to_ruby}\n#{@sequencer.clk_up}\nend"
2222
+ end
2223
+
2224
+ # Convert to C code.
2225
+ def to_c
2226
+ "\nwhile(#{@condition.to_c}) {\n#{@yes_blk.to_c}\n#{@sequencer.clk_up_c}\n}"
2227
+ end
2228
+ end
2229
+
2230
+ # Describes a SW implementation of a step statement.
2231
+ class Step
2232
+ # Create a new step statement in sequencer +sequencer+.
2233
+ def initialize(sequencer)
2234
+ @sequencer = sequencer
2235
+ end
2236
+
2237
+ # Convert to Ruby code.
2238
+ def to_ruby
2239
+ return @sequencer.clk_up
2240
+ end
2241
+
2242
+ # Convert to C code.
2243
+ def to_c
2244
+ return @sequencer.clk_up_c
2245
+ end
2246
+ end
2247
+
2248
+ # Describes a SW implementation of a break statement.
2249
+ class Sbreak
2250
+ # Create a new break statement in sequencer +sequencer+.
2251
+ def initialize(sequencer)
2252
+ @sequencer = sequencer
2253
+ end
2254
+
2255
+ # Convert to Ruby code.
2256
+ def to_ruby
2257
+ return @sequencer.clk_up + "\nbreak"
2258
+ end
2259
+
2260
+ # Convert to C code.
2261
+ def to_c
2262
+ return @sequencer.clk_up_c + "\nbreak;"
2263
+ end
2264
+ end
2265
+
2266
+ # Describes a SW implementation of a continue statement.
2267
+ class Scontinue
2268
+ # Create a new break statement in sequencer +sequencer+.
2269
+ def initialize
2270
+ @sequencer = sequencer
2271
+ end
2272
+
2273
+ # Convert to Ruby code.
2274
+ def to_ruby
2275
+ return @sequencer.clk_up + "\ncontinue"
2276
+ end
2277
+
2278
+ # Convert to Ruby code.
2279
+ def to_c
2280
+ return @sequencer.clk_up_c + "\ncontinue;"
2281
+ end
2282
+ end
2283
+
2284
+ # Describes a SW implementation of a terminate statement.
2285
+ class Sterminate
2286
+ # Create a new break statement in sequencer +sequencer+.
2287
+ def initialize
2288
+ @sequencer = sequencer
2289
+ end
2290
+
2291
+ # Convert to Ruby code.
2292
+ def to_ruby
2293
+ # Implemented as returning from a function since a sequencer
2294
+ # is implemented as one.
2295
+ return @sequencer.clk_up + "\nreturn"
2296
+ end
2297
+
2298
+ # Convert to C code.
2299
+ def to_c
2300
+ return @sequencer.clk_up_c + "\nreturn;"
2301
+ end
2302
+ end
2303
+
2304
+ # Describes a SW synchronization of a signal.
2305
+ class Sync
2306
+ def initialize
2307
+ end
2308
+
2309
+ # Convert to Ruby code.
2310
+ def to_ruby
2311
+ return "Fiber.yield"
2312
+ end
2313
+
2314
+ # Convert to C code.
2315
+ def to_c
2316
+ return "yield();"
2317
+ end
2318
+ end
2319
+
2320
+ # Describes arbitrary code.
2321
+ class Ruby < Expression
2322
+ @@ruby_blocks = []
2323
+
2324
+ # Create a new ruby code block for either +ruby_block+ or
2325
+ # string +str+.
2326
+ def initialize(str = nil, &ruby_block)
2327
+ @str = str
2328
+ # puts "ruby_block=#{ruby_block}"
2329
+ # Create the id for the block.
2330
+ @id = @@ruby_blocks.size
2331
+ # Adds the block.
2332
+ if ruby_block then
2333
+ @@ruby_blocks << ruby_block
2334
+ else
2335
+ @@ruby_blocks << proc { TOPLEVEL_BINDING.eval(@str.to_s) }
2336
+ end
2337
+ end
2338
+
2339
+ # Convert to expression: does not change but remove from the
2340
+ # statement list.
2341
+ def to_expr
2342
+ # Remove the transmit from the top SW block.
2343
+ RubyHDL::High.top_sblock.delete(self)
2344
+ return self
2345
+ end
2346
+
2347
+ # Execute a ruby code block for +ruby_block+.
2348
+ def self.call(id)
2349
+ # puts "id=#{id}"
2350
+ @@ruby_blocks[id].call
2351
+ end
2352
+
2353
+ # Convert to Ruby code.
2354
+ def to_ruby
2355
+ # puts caller[0]
2356
+ if @str then
2357
+ return TOPLEVEL_BINDING.eval(@str)
2358
+ else
2359
+ return "RubyHDL::High::Ruby.call(#{@id})"
2360
+ end
2361
+ end
2362
+
2363
+ # Convert to C code.
2364
+ def to_c
2365
+ return "rb_eval_string(\"#{@str.to_s}\");"
2366
+ end
2367
+ end
2368
+
2369
+
2370
+ # Describes a SW implementation of an call statement.
2371
+ class Scall
2372
+ using RubyHDL::High
2373
+
2374
+ # Create a new call statement in sequencer +sequencer+ for function
2375
+ # named +name+ with arguments +args+.
2376
+ def initialize(sequencer, name, *args)
2377
+ @sequencer = sequencer
2378
+ @name = name.to_sym
2379
+ @args = args
2380
+ end
2381
+
2382
+ # Convert to Ruby code.
2383
+ def to_ruby
2384
+ return @sequencer.clk_up + "\n#{name}(" +
2385
+ @args.map {|arg| arg.to_ruby}.join(",") + ")"
2386
+ end
2387
+
2388
+ # Convert to C code.
2389
+ def to_c
2390
+ return @sequencer.clk_up + "\n#{name}(" +
2391
+ @args.map {|arg| arg.to_ruby}.join(",") + ");"
2392
+ end
2393
+
2394
+ # Create an iterator for a given method +meth+.
2395
+ def make_iterator(meth,*args,&ruby_block)
2396
+ if ruby_block then
2397
+ blk = Sblock.new(@sequencer,&ruby_block)
2398
+ command = RubyHDL::High::Ruby.new do
2399
+ "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")}) { #{blk.to_ruby} }"
2400
+ end
2401
+ else
2402
+ command = RubyHDL::High::Ruby.new do
2403
+ "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")})"
2404
+ end
2405
+ end
2406
+ return Iter.new(@sequencer,*self.commands,command,&ruby_block)
2407
+ end
2408
+
2409
+ # The iterator methods.
2410
+
2411
+ # Iterator on each of the elements in range +rng+.
2412
+ # *NOTE*:
2413
+ # - Stop iteration when the end of the range is reached or when there
2414
+ # are no elements left
2415
+ # - This is not a method from Ruby but one specific for hardware where
2416
+ # creating a array is very expensive.
2417
+ def seach_range(rng,&ruby_block)
2418
+ return self.make_iterator("each_range",rng,&ruby_block)
2419
+ end
2420
+
2421
+ # Tell if all the elements respect a given criterion given either
2422
+ # as +arg+ or as block.
2423
+ def sall?(arg = nil,&ruby_block)
2424
+ return self.make_iterator("all?",arg,&ruby_block)
2425
+ end
2426
+
2427
+ # Tell if any of the elements respects a given criterion given either
2428
+ # as +arg+ or as block.
2429
+ def sany?(arg = nil,&ruby_block)
2430
+ return self.make_iterator("any?",arg,&ruby_block)
2431
+ end
2432
+
2433
+ # Returns an SEnumerator generated from current enumerable and +arg+
2434
+ def schain(arg)
2435
+ return self.make_iterator("chain",arg)
2436
+ end
2437
+
2438
+ # HW implementation of the Ruby chunk.
2439
+ # NOTE: to do, or may be not.
2440
+ def schunk(*args,&ruby_block)
2441
+ raise "schunk is not supported yet."
2442
+ end
2443
+
2444
+ # HW implementation of the Ruby chunk_while.
2445
+ # NOTE: to do, or may be not.
2446
+ def schunk_while(*args,&ruby_block)
2447
+ raise "schunk_while is not supported yet."
2448
+ end
2449
+
2450
+ # Returns a vector containing the execution result of the given block
2451
+ # on each element. If no block is given, return an SEnumerator.
2452
+ # NOTE: be carful that the resulting vector can become huge if there
2453
+ # are many element.
2454
+ def smap(&ruby_block)
2455
+ return self.make_iterator("map",&ruby_block)
2456
+ end
2457
+
2458
+ # HW implementation of the Ruby flat_map.
2459
+ # NOTE: actually due to the way HDLRuby handles vectors, should work
2460
+ # like smap
2461
+ def sflat_map(&ruby_block)
2462
+ return self.make_iterator("flat_map",&ruby_block)
2463
+ end
2464
+
2465
+ # HW implementation of the Ruby compact, but remove 0 values instead
2466
+ # on nil (since nil that does not have any meaning in HW).
2467
+ def scompact
2468
+ return self.make_iterator("compact",&ruby_block)
2469
+ end
2470
+
2471
+
2472
+ # WH implementation of the Ruby count.
2473
+ def scount(obj = nil, &ruby_block)
2474
+ return self.make_iterator("count",obj,&ruby_block)
2475
+ end
2476
+
2477
+ # HW implementation of the Ruby cycle.
2478
+ def scycle(n = nil,&ruby_block)
2479
+ return self.make_iterator("cycle",n,&ruby_block)
2480
+ end
2481
+
2482
+ # HW implementation of the Ruby find.
2483
+ # NOTE: contrary to Ruby, if_none_proc is mandatory since there is no
2484
+ # nil in HW. Moreover, the argument can also be a value.
2485
+ def sfind(if_none_proc, &ruby_block)
2486
+ return self.make_iterator("find",if_none_proc,&ruby_block)
2487
+ end
2488
+
2489
+ # HW implementation of the Ruby drop.
2490
+ def sdrop(n)
2491
+ return self.make_iterator("drop",n)
2492
+ end
2493
+
2494
+ # HW implementation of the Ruby drop_while.
2495
+ def sdrop_while(&ruby_block)
2496
+ return self.make_iterator("drop_while",&ruby_block)
2497
+ end
2498
+
2499
+ # HW implementation of the Ruby each_cons
2500
+ def seach_cons(n,&ruby_block)
2501
+ return self.make_iterator("each_cons",n,&ruby_block)
2502
+ end
2503
+
2504
+ # HW implementation of the Ruby each_entry.
2505
+ # NOTE: to do, or may be not.
2506
+ def seach_entry(*args,&ruby_block)
2507
+ raise "seach_entry is not supported yet."
2508
+ end
2509
+
2510
+ # HW implementation of the Ruby each_slice
2511
+ def seach_slice(n,&ruby_block)
2512
+ return self.make_iterator("each_slice",n,&ruby_block)
2513
+ end
2514
+
2515
+ # HW implementation of the Ruby each_with_index.
2516
+ def seach_with_index(*args,&ruby_block)
2517
+ return self.make_iterator("each_with_index",*args,&ruby_block)
2518
+ end
2519
+
2520
+ # HW implementation of the Ruby each_with_object.
2521
+ def seach_with_object(obj,&ruby_block)
2522
+ return self.make_iterator("each_with_object",obj,&ruby_block)
2523
+ end
2524
+
2525
+ # HW implementation of the Ruby to_a.
2526
+ def sto_a
2527
+ return self.make_iterator("to_a")
2528
+ end
2529
+
2530
+ # HW implementation of the Ruby select.
2531
+ def sselect(&ruby_block)
2532
+ return self.make_iterator("select",&ruby_block)
2533
+ end
2534
+
2535
+ # HW implementation of the Ruby find_index.
2536
+ def sfind_index(obj = nil, &ruby_block)
2537
+ return self.make_iterator("find_index",obj,&ruby_block)
2538
+ end
2539
+
2540
+ # HW implementation of the Ruby first.
2541
+ def sfirst(n=1)
2542
+ return self.make_iterator("first",n)
2543
+ end
2544
+
2545
+ # HW implementation of the Ruby grep.
2546
+ # NOTE: to do, or may be not.
2547
+ def sgrep(*args,&ruby_block)
2548
+ raise "sgrep is not supported yet."
2549
+ end
2550
+
2551
+ # HW implementation of the Ruby grep_v.
2552
+ # NOTE: to do, or may be not.
2553
+ def sgrep_v(*args,&ruby_block)
2554
+ raise "sgrep_v is not supported yet."
2555
+ end
2556
+
2557
+ # HW implementation of the Ruby group_by.
2558
+ # NOTE: to do, or may be not.
2559
+ def sgroup_by(*args,&ruby_block)
2560
+ raise "sgroup_by is not supported yet."
2561
+ end
2562
+
2563
+ # HW implementation of the Ruby include?
2564
+ def sinclude?(obj)
2565
+ return self.make_iterator("include?",obj)
2566
+ end
2567
+
2568
+ # HW implementation of the Ruby inject.
2569
+ def sinject(*args,&ruby_block)
2570
+ return self.make_iterator("inject",*args,&ruby_block)
2571
+ end
2572
+
2573
+ # HW implementation of the Ruby reduce.
2574
+ def sreduce
2575
+ return self.make_iterator("reduce",*args,&ruby_block)
2576
+ end
2577
+
2578
+
2579
+ # HW implementation of the Ruby lazy.
2580
+ # NOTE: to do, or may be not.
2581
+ def slazy(*args,&ruby_block)
2582
+ raise "slazy is not supported yet."
2583
+ end
2584
+
2585
+ # HW implementation of the Ruby max.
2586
+ def smax(n = nil, &ruby_block)
2587
+ return self.make_iterator("max",n,&ruby_block)
2588
+ end
2589
+
2590
+ # HW implementation of the Ruby max_by.
2591
+ def smax_by(n = nil, &ruby_block)
2592
+ return self.make_iterator("max_by",n,&ruby_block)
2593
+ end
2594
+
2595
+ # HW implementation of the Ruby min.
2596
+ def smin(n = nil, &ruby_block)
2597
+ return self.make_iterator("min",n,&ruby_block)
2598
+ end
2599
+
2600
+ # HW implementation of the Ruby min_by.
2601
+ def smin_by(n = nil, &ruby_block)
2602
+ return self.make_iterator("min_by",n,&ruby_block)
2603
+ end
2604
+
2605
+ # HW implementation of the Ruby minmax.
2606
+ def sminmax(&ruby_block)
2607
+ return self.make_iterator("minmax",&ruby_block)
2608
+ end
2609
+
2610
+ # HW implementation of the Ruby minmax_by.
2611
+ def sminmax_by(&ruby_block)
2612
+ return self.make_iterator("minmax_by",&ruby_block)
2613
+ end
2614
+
2615
+ # Tell if none of the elements respects a given criterion given either
2616
+ # as +arg+ or as block.
2617
+ def snone?(arg = nil,&ruby_block)
2618
+ return self.make_iterator("none?",arg,&ruby_block)
2619
+ end
2620
+
2621
+ # Tell if one and only one of the elements respects a given criterion
2622
+ # given either as +arg+ or as block.
2623
+ def sone?(arg = nil,&ruby_block)
2624
+ return self.make_iterator("one?",arg,&ruby_block)
2625
+ end
2626
+
2627
+ # HW implementation of the Ruby partition.
2628
+ # NOTE: to do, or may be not.
2629
+ def spartition(*args,&ruby_block)
2630
+ raise "spartition is not supported yet."
2631
+ end
2632
+
2633
+ # HW implementatiob of the Ruby reject.
2634
+ def sreject(&ruby_block)
2635
+ return self.make_iterator("reject",&ruby_block)
2636
+ end
2637
+
2638
+ # HW implementatiob of the Ruby reverse_each.
2639
+ def sreverse_each(*args,&ruby_block)
2640
+ return self.make_iterator("reverse_each",*args,&ruby_block)
2641
+ end
2642
+
2643
+ # HW implementation of the Ruby slice_after.
2644
+ # NOTE: to do, or may be not.
2645
+ def sslice_after(pattern = nil,&ruby_block)
2646
+ raise "sslice_after is not supported yet."
2647
+ end
2648
+
2649
+ # HW implementation of the Ruby slice_before.
2650
+ # NOTE: to do, or may be not.
2651
+ def sslice_before(*args,&ruby_block)
2652
+ raise "sslice_before is not supported yet."
2653
+ end
2654
+
2655
+ # HW implementation of the Ruby slice_when.
2656
+ # NOTE: to do, or may be not.
2657
+ def sslice_when(*args,&ruby_block)
2658
+ raise "sslice_before is not supported yet."
2659
+ end
2660
+
2661
+ # Merge two arrays in order, for ssort only.
2662
+ def ssort_merge(arI, arO, first, middle, last, &ruby_block)
2663
+ return self.make_iterator("sort_merge",arI,arO,first,middle,last,&ruby_block)
2664
+ end
2665
+
2666
+ # HW implementation of the Ruby sort.
2667
+ def ssort(&ruby_block)
2668
+ return self.make_iterator("sort",&ruby_block)
2669
+ end
2670
+
2671
+ # HW implementation of the Ruby sort.
2672
+ def ssort_by(&ruby_block)
2673
+ return self.make_iterator("sort_by",&ruby_block)
2674
+ end
2675
+
2676
+ # HW implementation of the Ruby sum.
2677
+ def ssum(initial_value = nil,&ruby_block)
2678
+ return self.make_iterator("sum",initial_value,&ruby_block)
2679
+ end
2680
+
2681
+ # The HW implementation of the Ruby take.
2682
+ def stake(n)
2683
+ return self.make_iterator("take",n)
2684
+ end
2685
+
2686
+ # The HW implementation of the Ruby take_while.
2687
+ def stake_while(&ruby_block)
2688
+ return self.make_iterator("take_while",&ruby_block)
2689
+ end
2690
+
2691
+ # HW implementation of the Ruby tally.
2692
+ # NOTE: to do, or may be not.
2693
+ def stally(h = nil)
2694
+ raise "stally is not supported yet."
2695
+ end
2696
+
2697
+ # HW implementation of the Ruby to_h.
2698
+ # NOTE: to do, or may be not.
2699
+ def sto_h(h = nil)
2700
+ raise "sto_h is not supported yet."
2701
+ end
2702
+
2703
+ # HW implementation of the Ruby uniq.
2704
+ def suniq(&ruby_block)
2705
+ return self.make_iterator("uniq",&ruby_block)
2706
+ end
2707
+
2708
+ # HW implementation of the Ruby zip.
2709
+ # NOTE: for now szip is deactivated untile tuples are properly
2710
+ # handled by HDLRuby.
2711
+ def szip(obj,&ruby_block)
2712
+ return self.make_iterator("zip",obj,&ruby_block)
2713
+ end
2714
+
2715
+ # Iterator on the +num+ next elements.
2716
+ # *NOTE*:
2717
+ # - Stop iteration when the end of the range is reached or when there
2718
+ # are no elements left
2719
+ # - This is not a method from Ruby but one specific for hardware where
2720
+ # creating a array is very expensive.
2721
+ def seach_nexts(num,&ruby_block)
2722
+ return self.seach.snexts(num,&ruby_block)
2723
+ end
2724
+ end
2725
+
2726
+
2727
+ # Describes a SW implementation of an iterator statement.
2728
+ class Siter
2729
+ using RubyHDL::High
2730
+
2731
+ # Create a new iteration statement in sequencer +sequencer+
2732
+ # for chain of commands +commands+ to interate while
2733
+ # executing +ruby_block+.
2734
+ def initialize(sequencer,*commands, &ruby_block)
2735
+ @sequencer = sequencer
2736
+ @commands = commands
2737
+ if ruby_block then
2738
+ # The iterator is finalized.
2739
+ @blk = Sblock.new(sequencer,&ruby_block)
2740
+ end
2741
+ # puts "New iterator with blk=#{@blk} commands=#{@commands}"
2742
+ end
2743
+
2744
+ # Iterate on the commands.
2745
+ def each_command(&ruby_block)
2746
+ return to_enum(:each_command) unless ruby_block
2747
+ @commands.each(&ruby_block)
2748
+ end
2749
+ alias_method :each, :each_command
2750
+
2751
+ # Convert to Ruby code.
2752
+ def to_ruby
2753
+ # puts "to_ruby with blk=#{@blk} commands=#{@commands}"
2754
+ res = @sequencer.clk_up + "\n" +
2755
+ @commands.map { |command| command.to_ruby }.join(".")
2756
+ return res + " do\n#{@blk.to_ruby}\n#{@sequencer.clk_up}\nend"
2757
+ end
2758
+
2759
+ # Convert to C code.
2760
+ def to_c
2761
+ res = @sequencer.clk_up_c + "\n" +
2762
+ @commands.map { |command| command.to_c }.join("_")
2763
+ return res + "(#{@blk.to_c})"
2764
+ end
2765
+
2766
+ # Create an iterator for a given method +meth+.
2767
+ def make_iterator(meth,*args,&ruby_block)
2768
+ # if ruby_block then
2769
+ # blk = Sblock.new(@sequencer,&ruby_block)
2770
+ # command = RubyHDL::High::Ruby.new do
2771
+ # "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")}) { #{blk.to_ruby} }"
2772
+ # end
2773
+ # else
2774
+ # command = RubyHDL::High::Ruby.new do
2775
+ # "#{meth}(#{args.map{|arg| arg.to_ruby}.join(",")})"
2776
+ # end
2777
+ # end
2778
+ command = "#{meth}"
2779
+ if args.any? then
2780
+ command += "(*#{RubyHDL::High::Ruby.new {
2781
+ "#{args.map{|arg| arg.to_ruby}.join(",")}"}})"
2782
+ end
2783
+ return Siter.new(@sequencer,*@commands,command,&ruby_block)
2784
+ end
2785
+
2786
+ # The iterator methods.
2787
+
2788
+ # Iterator on each of the elements in range +rng+.
2789
+ # *NOTE*:
2790
+ # - Stop iteration when the end of the range is reached or when there
2791
+ # are no elements left
2792
+ # - This is not a method from Ruby but one specific for hardware where
2793
+ # creating a array is very expensive.
2794
+ def seach_range(rng,&ruby_block)
2795
+ return self.make_iterator("each_range",rng,&ruby_block)
2796
+ end
2797
+
2798
+ # Tell if all the elements respect a given criterion given either
2799
+ # as +arg+ or as block.
2800
+ def sall?(arg = nil,&ruby_block)
2801
+ return self.make_iterator("all?",arg,&ruby_block)
2802
+ end
2803
+
2804
+ # Tell if any of the elements respects a given criterion given either
2805
+ # as +arg+ or as block.
2806
+ def sany?(arg = nil,&ruby_block)
2807
+ return self.make_iterator("any?",arg,&ruby_block)
2808
+ end
2809
+
2810
+ # Returns an SEnumerator generated from current enumerable and +arg+
2811
+ def schain(arg)
2812
+ return self.make_iterator("chain",arg)
2813
+ end
2814
+
2815
+ # HW implementation of the Ruby chunk.
2816
+ # NOTE: to do, or may be not.
2817
+ def schunk(*args,&ruby_block)
2818
+ raise "schunk is not supported yet."
2819
+ end
2820
+
2821
+ # HW implementation of the Ruby chunk_while.
2822
+ # NOTE: to do, or may be not.
2823
+ def schunk_while(*args,&ruby_block)
2824
+ raise "schunk_while is not supported yet."
2825
+ end
2826
+
2827
+ # Returns a vector containing the execution result of the given block
2828
+ # on each element. If no block is given, return an SEnumerator.
2829
+ # NOTE: be carful that the resulting vector can become huge if there
2830
+ # are many element.
2831
+ def smap(&ruby_block)
2832
+ return self.make_iterator("map",&ruby_block)
2833
+ end
2834
+
2835
+ # HW implementation of the Ruby flat_map.
2836
+ # NOTE: actually due to the way HDLRuby handles vectors, should work
2837
+ # like smap
2838
+ def sflat_map(&ruby_block)
2839
+ return self.make_iterator("flat_map",&ruby_block)
2840
+ end
2841
+
2842
+ # HW implementation of the Ruby compact, but remove 0 values instead
2843
+ # on nil (since nil that does not have any meaning in HW).
2844
+ def scompact
2845
+ return self.make_iterator("compact",&ruby_block)
2846
+ end
2847
+
2848
+
2849
+ # WH implementation of the Ruby count.
2850
+ def scount(obj = nil, &ruby_block)
2851
+ return self.make_iterator("count",obj,&ruby_block)
2852
+ end
2853
+
2854
+ # HW implementation of the Ruby cycle.
2855
+ def scycle(n = nil,&ruby_block)
2856
+ return self.make_iterator("cycle",n,&ruby_block)
2857
+ end
2858
+
2859
+ # HW implementation of the Ruby find.
2860
+ # NOTE: contrary to Ruby, if_none_proc is mandatory since there is no
2861
+ # nil in HW. Moreover, the argument can also be a value.
2862
+ def sfind(if_none_proc, &ruby_block)
2863
+ return self.make_iterator("find",if_none_proc,&ruby_block)
2864
+ end
2865
+
2866
+ # HW implementation of the Ruby drop.
2867
+ def sdrop(n)
2868
+ return self.make_iterator("drop",n)
2869
+ end
2870
+
2871
+ # HW implementation of the Ruby drop_while.
2872
+ def sdrop_while(&ruby_block)
2873
+ return self.make_iterator("drop_while",&ruby_block)
2874
+ end
2875
+
2876
+ # HW implementation of the Ruby each_cons
2877
+ def seach_cons(n,&ruby_block)
2878
+ return self.make_iterator("each_cons",n,&ruby_block)
2879
+ end
2880
+
2881
+ # HW implementation of the Ruby each_entry.
2882
+ # NOTE: to do, or may be not.
2883
+ def seach_entry(*args,&ruby_block)
2884
+ raise "seach_entry is not supported yet."
2885
+ end
2886
+
2887
+ # HW implementation of the Ruby each_slice
2888
+ def seach_slice(n,&ruby_block)
2889
+ return self.make_iterator("each_slice",n,&ruby_block)
2890
+ end
2891
+
2892
+ # HW implementation of the Ruby each_with_index.
2893
+ def seach_with_index(*args,&ruby_block)
2894
+ return self.make_iterator("each_with_index",*args,&ruby_block)
2895
+ end
2896
+ alias_method :with_index, :seach_with_index
2897
+
2898
+ # HW implementation of the Ruby each_with_object.
2899
+ def seach_with_object(obj,&ruby_block)
2900
+ return self.make_iterator("each_with_object",obj,&ruby_block)
2901
+ end
2902
+
2903
+ # HW implementation of the Ruby to_a.
2904
+ def sto_a
2905
+ return self.make_iterator("to_a")
2906
+ end
2907
+
2908
+ # HW implementation of the Ruby select.
2909
+ def sselect(&ruby_block)
2910
+ return self.make_iterator("select",&ruby_block)
2911
+ end
2912
+
2913
+ # HW implementation of the Ruby find_index.
2914
+ def sfind_index(obj = nil, &ruby_block)
2915
+ return self.make_iterator("find_index",obj,&ruby_block)
2916
+ end
2917
+
2918
+ # HW implementation of the Ruby first.
2919
+ def sfirst(n=1)
2920
+ return self.make_iterator("first",n)
2921
+ end
2922
+
2923
+ # HW implementation of the Ruby grep.
2924
+ # NOTE: to do, or may be not.
2925
+ def sgrep(*args,&ruby_block)
2926
+ raise "sgrep is not supported yet."
2927
+ end
2928
+
2929
+ # HW implementation of the Ruby grep_v.
2930
+ # NOTE: to do, or may be not.
2931
+ def sgrep_v(*args,&ruby_block)
2932
+ raise "sgrep_v is not supported yet."
2933
+ end
2934
+
2935
+ # HW implementation of the Ruby group_by.
2936
+ # NOTE: to do, or may be not.
2937
+ def sgroup_by(*args,&ruby_block)
2938
+ raise "sgroup_by is not supported yet."
2939
+ end
2940
+
2941
+ # HW implementation of the Ruby include?
2942
+ def sinclude?(obj)
2943
+ return self.make_iterator("include?",obj)
2944
+ end
2945
+
2946
+ # HW implementation of the Ruby inject.
2947
+ def sinject(*args,&ruby_block)
2948
+ return self.make_iterator("inject",*args,&ruby_block)
2949
+ end
2950
+
2951
+ # HW implementation of the Ruby reduce.
2952
+ def sreduce
2953
+ return self.make_iterator("reduce",*args,&ruby_block)
2954
+ end
2955
+
2956
+
2957
+ # HW implementation of the Ruby lazy.
2958
+ # NOTE: to do, or may be not.
2959
+ def slazy(*args,&ruby_block)
2960
+ raise "slazy is not supported yet."
2961
+ end
2962
+
2963
+ # HW implementation of the Ruby max.
2964
+ def smax(n = nil, &ruby_block)
2965
+ return self.make_iterator("max",n,&ruby_block)
2966
+ end
2967
+
2968
+ # HW implementation of the Ruby max_by.
2969
+ def smax_by(n = nil, &ruby_block)
2970
+ return self.make_iterator("max_by",n,&ruby_block)
2971
+ end
2972
+
2973
+ # HW implementation of the Ruby min.
2974
+ def smin(n = nil, &ruby_block)
2975
+ return self.make_iterator("min",n,&ruby_block)
2976
+ end
2977
+
2978
+ # HW implementation of the Ruby min_by.
2979
+ def smin_by(n = nil, &ruby_block)
2980
+ return self.make_iterator("min_by",n,&ruby_block)
2981
+ end
2982
+
2983
+ # HW implementation of the Ruby minmax.
2984
+ def sminmax(&ruby_block)
2985
+ return self.make_iterator("minmax",&ruby_block)
2986
+ end
2987
+
2988
+ # HW implementation of the Ruby minmax_by.
2989
+ def sminmax_by(&ruby_block)
2990
+ return self.make_iterator("minmax_by",&ruby_block)
2991
+ end
2992
+
2993
+ # Tell if none of the elements respects a given criterion given either
2994
+ # as +arg+ or as block.
2995
+ def snone?(arg = nil,&ruby_block)
2996
+ return self.make_iterator("none?",arg,&ruby_block)
2997
+ end
2998
+
2999
+ # Tell if one and only one of the elements respects a given criterion
3000
+ # given either as +arg+ or as block.
3001
+ def sone?(arg = nil,&ruby_block)
3002
+ return self.make_iterator("one?",arg,&ruby_block)
3003
+ end
3004
+
3005
+ # HW implementation of the Ruby partition.
3006
+ # NOTE: to do, or may be not.
3007
+ def spartition(*args,&ruby_block)
3008
+ raise "spartition is not supported yet."
3009
+ end
3010
+
3011
+ # HW implementatiob of the Ruby reject.
3012
+ def sreject(&ruby_block)
3013
+ return self.make_iterator("reject",&ruby_block)
3014
+ end
3015
+
3016
+ # HW implementatiob of the Ruby reverse_each.
3017
+ def sreverse_each(*args,&ruby_block)
3018
+ return self.make_iterator("reverse_each",*args,&ruby_block)
3019
+ end
3020
+
3021
+ # HW implementation of the Ruby slice_after.
3022
+ # NOTE: to do, or may be not.
3023
+ def sslice_after(pattern = nil,&ruby_block)
3024
+ raise "sslice_after is not supported yet."
3025
+ end
3026
+
3027
+ # HW implementation of the Ruby slice_before.
3028
+ # NOTE: to do, or may be not.
3029
+ def sslice_before(*args,&ruby_block)
3030
+ raise "sslice_before is not supported yet."
3031
+ end
3032
+
3033
+ # HW implementation of the Ruby slice_when.
3034
+ # NOTE: to do, or may be not.
3035
+ def sslice_when(*args,&ruby_block)
3036
+ raise "sslice_before is not supported yet."
3037
+ end
3038
+
3039
+ # Merge two arrays in order, for ssort only.
3040
+ def ssort_merge(arI, arO, first, middle, last, &ruby_block)
3041
+ return self.make_iterator("sort_merge",arI,arO,first,middle,last,&ruby_block)
3042
+ end
3043
+
3044
+ # HW implementation of the Ruby sort.
3045
+ def ssort(&ruby_block)
3046
+ return self.make_iterator("sort",&ruby_block)
3047
+ end
3048
+
3049
+ # HW implementation of the Ruby sort.
3050
+ def ssort_by(&ruby_block)
3051
+ return self.make_iterator("sort_by",&ruby_block)
3052
+ end
3053
+
3054
+ # HW implementation of the Ruby sum.
3055
+ def ssum(initial_value = nil,&ruby_block)
3056
+ return self.make_iterator("sum",initial_value,&ruby_block)
3057
+ end
3058
+
3059
+ # The HW implementation of the Ruby take.
3060
+ def stake(n)
3061
+ return self.make_iterator("take",n)
3062
+ end
3063
+
3064
+ # The HW implementation of the Ruby take_while.
3065
+ def stake_while(&ruby_block)
3066
+ return self.make_iterator("take_while",&ruby_block)
3067
+ end
3068
+
3069
+ # HW implementation of the Ruby tally.
3070
+ # NOTE: to do, or may be not.
3071
+ def stally(h = nil)
3072
+ raise "stally is not supported yet."
3073
+ end
3074
+
3075
+ # HW implementation of the Ruby to_h.
3076
+ # NOTE: to do, or may be not.
3077
+ def sto_h(h = nil)
3078
+ raise "sto_h is not supported yet."
3079
+ end
3080
+
3081
+ # HW implementation of the Ruby uniq.
3082
+ def suniq(&ruby_block)
3083
+ return self.make_iterator("uniq",&ruby_block)
3084
+ end
3085
+
3086
+ # HW implementation of the Ruby zip.
3087
+ # NOTE: for now szip is deactivated untile tuples are properly
3088
+ # handled by HDLRuby.
3089
+ def szip(obj,&ruby_block)
3090
+ return self.make_iterator("zip",obj,&ruby_block)
3091
+ end
3092
+
3093
+ # Iterator on the +num+ next elements.
3094
+ # *NOTE*:
3095
+ # - Stop iteration when the end of the range is reached or when there
3096
+ # are no elements left
3097
+ # - This is not a method from Ruby but one specific for hardware where
3098
+ # creating a array is very expensive.
3099
+ def seach_nexts(num,&ruby_block)
3100
+ return self.seach.snexts(num,&ruby_block)
3101
+ end
3102
+ end
3103
+
3104
+
3105
+ # Describes a SW implementation of a signal.
3106
+ class SignalI < Expression
3107
+ using RubyHDL::High
3108
+ attr_reader :type, :name #, :content
3109
+ # Create a new signal with type +type+ and name +name+.
3110
+ def initialize(type,name)
3111
+ @type = type.to_type
3112
+ @name = name.to_sym
3113
+ end
3114
+
3115
+ # Tell if the signal is an array.
3116
+ def array?
3117
+ return @type.base.is_a?(TypeVector)
3118
+ end
3119
+
3120
+ # Convert to Ruby code.
3121
+ def to_ruby
3122
+ return "__" + self.name.to_s
3123
+ end
3124
+
3125
+ # Convert to C code.
3126
+ alias_method :to_c, :to_ruby
3127
+
3128
+ # Check if a value is defined for the signal.
3129
+ def value?
3130
+ return TOPLEVEL_BINDING.local_variable_defined?(self.to_ruby)
3131
+ end
3132
+
3133
+ # Gets the value of the signal.
3134
+ def value
3135
+ return TOPLEVEL_BINDING.eval(self.to_ruby)
3136
+ end
3137
+
3138
+ # Sets the value of the signal.
3139
+ def value=(val)
3140
+ return TOPLEVEL_BINDING.eval("#{self.to_ruby} = #{val}")
3141
+ end
3142
+
3143
+ # Convert to an integer.
3144
+ def to_i
3145
+ return self.value.to_i
3146
+ end
3147
+
3148
+ # Convert to an float.
3149
+ def to_f
3150
+ return self.value.to_f
3151
+ end
3152
+
3153
+ # Convert to a string.
3154
+ def to_s
3155
+ return self.value.to_s
3156
+ end
3157
+ end
3158
+
3159
+
3160
+ # Describes a SW implementation of a block.
3161
+ class Sblock < SblockTop
3162
+ using RubyHDL::High
3163
+
3164
+ attr_reader :sequencer
3165
+
3166
+ # Create a new block for sequencer +sequencer+ and fill it by
3167
+ # executing +ruby_block+
3168
+ def initialize(sequencer,&ruby_block)
3169
+ super()
3170
+ # Sets the sequencer.
3171
+ @sequencer = sequencer
3172
+ # Initialize the statements.
3173
+ @statements = []
3174
+ # Push the new sblock on top of the stack.
3175
+ RubyHDL::High.push_sblock(self)
3176
+ # Make signals from the arguments of the ruby block.
3177
+ @args = []
3178
+ ruby_block.parameters.each do |typ,arg|
3179
+ @args << SignalI.new(Void,arg)
3180
+ end
3181
+ # Fill it.
3182
+ self.instance_exec(*@args,&ruby_block)
3183
+ # Pop the new sblock.
3184
+ RubyHDL::High.pop_sblock
3185
+ end
3186
+
3187
+ # Add a new statement to the block.
3188
+ def add(statement)
3189
+ # Add the statement.
3190
+ @statements.push(statement)
3191
+ # # If the statement is a transmit, schedule the corresponding
3192
+ # # signal for final update.
3193
+ # if statement.is_a?(Transmit) then
3194
+ # @sequencer.to_update(statement.left)
3195
+ # end
3196
+ statement
3197
+ end
3198
+ alias_method :<<, :add
3199
+
3200
+ # Delete a statement.
3201
+ def delete(statement)
3202
+ @statements.delete(statement)
3203
+ end
3204
+
3205
+ # Unshift a new statement.
3206
+ def unshift(statement)
3207
+ @statements.unshift(statement)
3208
+ end
3209
+
3210
+ # Get the last statement.
3211
+ def last_statement
3212
+ return @statements[-1]
3213
+ end
3214
+
3215
+
3216
+ # Convert to Ruby code.
3217
+ def to_ruby
3218
+ res = ""
3219
+ # Generate the arguments if any.
3220
+ if @args.any? then
3221
+ res = "|#{@args.map(&:to_ruby).join(",")}|\n"
3222
+ end
3223
+ # Generate the statements.
3224
+ res += @statements.map do |stmnt|
3225
+ stmnt.to_ruby + "\n"
3226
+ end.join
3227
+ return res
3228
+ end
3229
+
3230
+ # Convert to C code.
3231
+ def to_c
3232
+ res = ""
3233
+ # Generate the arguments if any.
3234
+ if @args.any? then
3235
+ res = "(#{@args.map(&:to_c).join(",")})\n"
3236
+ end
3237
+ # Generate the statements.
3238
+ res += "{" + @statements.map do |stmnt|
3239
+ stmnt.to_ruby + "\n"
3240
+ end.join + "}"
3241
+ return res
3242
+ end
3243
+
3244
+ # The interface for describing statements and expressions.
3245
+
3246
+ # Mark a step.
3247
+ def step
3248
+ self << RubyHDL::High::Step.new(@sequencer)
3249
+ end
3250
+
3251
+ # Mark several steps.
3252
+ def steps(num)
3253
+ num.times { self.step }
3254
+ end
3255
+
3256
+ # Breaks current iteration.
3257
+ def sbreak
3258
+ self << RubyHDL::High::Sbreak.new(@sequencer)
3259
+ end
3260
+
3261
+ # Continues current iteration.
3262
+ def scontinue
3263
+ self << RubyHDL::High::Scontinue.new(@sequencer)
3264
+ end
3265
+
3266
+ # Terminates the sequencer.
3267
+ def sterminate
3268
+ self << RubyHDL::High::Sterminate.new(@sequencer)
3269
+ end
3270
+
3271
+ # Create a sequential if statement on +cond+.
3272
+ def sif(cond, &ruby_block)
3273
+ self << RubyHDL::High::Sif.new(@sequencer,cond,&ruby_block)
3274
+ end
3275
+
3276
+ # Create a sequential elsif statement on +cond+.
3277
+ def selsif(cond, &ruby_block)
3278
+ self.last_statement.selsif(&ruby_block)
3279
+ end
3280
+
3281
+ # Create a sequential else statement.
3282
+ def selse(&ruby_block)
3283
+ self.last_statement.selse(&ruby_block)
3284
+ end
3285
+
3286
+ # Wait a given condition.
3287
+ def swait(cond)
3288
+ self.swhile(~cond)
3289
+ end
3290
+
3291
+ # Create a sequential while statement on +cond+.
3292
+ def swhile(cond,&ruby_block)
3293
+ self << RubyHDL::High::Swhile.new(@sequencer,cond,&ruby_block)
3294
+ end
3295
+
3296
+ # Create a sequential infinite loop statement.
3297
+ def sloop(&ruby_block)
3298
+ self << RubyHDL::High::Sloop.new(@sequencer,&ruby_block)
3299
+ end
3300
+
3301
+ # Create a sequential for statement iterating over the elements
3302
+ # of +expr+.
3303
+ def sfor(expr,&ruby_block)
3304
+ # Ensures there is a ruby block to avoid returning an enumerator
3305
+ # (returning an enumerator would be confusing for a for statement).
3306
+ ruby_block = proc {} unless ruby_block
3307
+ self << expr.seach.with_index(&ruby_block)
3308
+ end
3309
+
3310
+ # The SW-specific statements and expressions.
3311
+
3312
+ # Mark a synchronisation.
3313
+ # For software only: stop the current sequencer for allowing
3314
+ # sharing of variables with other ones.
3315
+ def sync
3316
+ self << RubyHDL::High::Sync.new
3317
+ end
3318
+
3319
+ # Some arbirary Ruby code as a string +str+ or as a proc
3320
+ # +ruby_block+.
3321
+ def ruby(str = nil, &ruby_block)
3322
+ self << RubyHDL::High::Ruby.new(str,&ruby_block)
3323
+ end
3324
+ end
3325
+
3326
+
3327
+ # Describes a SW implementation of a sequencer function.
3328
+ class SfunctionT
3329
+ using RubyHDL::High
3330
+ attr_reader :name
3331
+ # Create a new named +name+ with arguments +args+ and
3332
+ # executing the content of block +sblock+
3333
+ def initialize(type,name,sblock,*args)
3334
+ @name = name.to_sym
3335
+ @args = args
3336
+ @blk = sblock
3337
+ end
3338
+
3339
+ # Convert to Ruby code.
3340
+ def to_ruby
3341
+ return "def #{name}(#{@args.map {|arg| arg.to_ruby}.join(",")}\n#{@blk.to_ruby}\nend\n"
3342
+ end
3343
+
3344
+ # Convert to C code.
3345
+ def to_c
3346
+ return "unsigned long long #{name}(#{@args.map {|arg| "unsigned long long " + arg.to_c}.join(",")} {\n#{@blk.to_c}\n}\n"
3347
+ end
3348
+ end
3349
+
3350
+
3351
+
3352
+
3353
+ # Describes a SW implementation of a sequencer.
3354
+ class SequencerT
3355
+
3356
+ # The source code (in ruby).
3357
+ attr_reader :source
3358
+
3359
+ # The clock counter.
3360
+ attr_reader :clk
3361
+
3362
+ # Create a new sequencer block, with clock counter +clk+ and
3363
+ # run control +start+.
3364
+ # Note: if +clk+ is not provided no clock counter will be generate.
3365
+ def initialize(clk = nil, start = nil, &ruby_block)
3366
+ # Sets the clock counter and start control.
3367
+ @clk = clk
3368
+ @start = start
3369
+ if @start then
3370
+ this = self
3371
+ # Make @start a controlling signal.
3372
+ @start.define_singleton_method(:<=,val) do
3373
+ this.resume if val.to_i == 1
3374
+ end
3375
+ end
3376
+ # Create a set of sfunction used in the sequencer.
3377
+ @sfunctions = {}
3378
+ # Create the main block.
3379
+ @sblock = RubyHDL::High::Sblock.new(self,&ruby_block)
3380
+ # Build the Ruby code.
3381
+ @source = ""
3382
+ @code = nil
3383
+ self.build_ruby
3384
+ end
3385
+
3386
+ # Add a sfunction.
3387
+ def add_sfunction(name,sfunction)
3388
+ @sfunctions[name.to_sym] = sfunction
3389
+ end
3390
+
3391
+ # Check if a sfunction is present.
3392
+ def sfunction?(name)
3393
+ return @sfunctions.key?(name.to_sym)
3394
+ end
3395
+
3396
+ # Get a sfunction by name.
3397
+ def sfunction(name)
3398
+ return @sfunctions[name.to_sym]
3399
+ end
3400
+
3401
+ # Build the ruby code.
3402
+ def build_ruby
3403
+ this = self
3404
+ @source = <<-BUILD
3405
+ #{RubyHDL::High.global_sblock.each_signal.map do |signal|
3406
+ signal.to_ruby + " ||= " +
3407
+ (signal.array? ? "[]" : signal.value? ? signal.value.inspect : "nil")
3408
+ end.join("\n")}
3409
+ Fiber.new do
3410
+ #{@sblock.to_ruby}
3411
+ end
3412
+ BUILD
3413
+ # puts "building code_txt=" + @source
3414
+ @code = TOPLEVEL_BINDING.eval(@source)
3415
+ end
3416
+
3417
+ # Get the Ruby code.
3418
+ def to_ruby
3419
+ return @code
3420
+ end
3421
+
3422
+ # Convert to C code.
3423
+ def to_c
3424
+ typ = nil
3425
+ res = <<-BUILDC
3426
+ #{RubyHDL::High.global_sblock.each_signal.map do |signal|
3427
+ typ = signal.type
3428
+ typ.to_c + " " + signal.to_c + "=" + typ.to_c_init + ";"
3429
+ end.join("\n")}
3430
+ #{sblock.to_c}
3431
+ BUILDC
3432
+ return res
3433
+ end
3434
+
3435
+ # Handling of the clock if any.
3436
+
3437
+ # Generate a clock up of +count+ cycles if any clock.
3438
+ def clk_up(count = 1)
3439
+ if @clk then
3440
+ return "#{clk.to_ruby} += #{count.to_i}"
3441
+ else
3442
+ return ""
3443
+ end
3444
+ end
3445
+
3446
+ # Generate a clock up of +count+ cycles if any clock for C code.
3447
+ def clk_up_c(count = 1)
3448
+ if @clk then
3449
+ return "#{clk.to_c} += #{count.to_i};"
3450
+ else
3451
+ return ""
3452
+ end
3453
+ end
3454
+
3455
+
3456
+ # Control of the sequencer.
3457
+
3458
+ # Executes the sequencer.
3459
+ def resume
3460
+ # @code.call
3461
+ @code.resume
3462
+ end
3463
+ alias_method :call, :resume
3464
+
3465
+ # Check is the sequencer can still be resumed.
3466
+ def alive?
3467
+ @code.alive?
3468
+ end
3469
+
3470
+ end
3471
+
3472
+
3473
+
3474
+
3475
+
3476
+ # Create a new sequencer block, with clock counter +clk+ and
3477
+ # run control +start+
3478
+ def sequencer(clk = nil, start = nil, &ruby_block)
3479
+ return SequencerT.new(clk,start,&ruby_block)
3480
+ end
3481
+
3482
+ # Create a new function named +name+, built using block +ruby_block+.
3483
+ def sdef(name,&ruby_block)
3484
+ name = name.to_sym
3485
+ # Get the arguments of the ruby_block.
3486
+ args = ruby_block.parameters.map {|typ,name| name.to_s }
3487
+ # Register the call to the function.
3488
+ RubyHDL::High.register(name.to_sym) do |args|
3489
+ cur_seq = Ruby::HDL::High.top_sblock.sequencer
3490
+ unless cur_seq.sfunction?(name) then
3491
+ # Need to create a new sfunction.
3492
+ # Push a new empty sblock for building the function.
3493
+ RubyHDL::High.push_sblock(SblockTop.new)
3494
+ args.each do |arg|
3495
+ RubyHDL::High.top_sblock.make_inners(Void,arg.to_sym)
3496
+ end
3497
+ # Execute the ruby block in a sequencer environment for building
3498
+ # the sblock.
3499
+ sblock = Sblock.new(cur_seq,&ruby_block)
3500
+ RubyHDL::High.pop_sblock
3501
+ # Create the function.
3502
+ function = SfunctionT.new(name,args,sblock)
3503
+ # Add it to the sequencer.
3504
+ cur_seq.add_sfunction(name)
3505
+ end
3506
+ Scall.new(cur_seq,name,args)
3507
+ end
3508
+ end
3509
+
3510
+
3511
+ end
3512
+
3513
+