HDLRuby 3.8.1 → 3.9.0

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.
@@ -179,41 +179,6 @@ module RubyHDL::High
179
179
  method_missing(m,*args,&ruby_block)
180
180
  end
181
181
 
182
- # # Handling of new names.
183
- # class ::Object
184
- # alias_method :old_method_missing, :method_missing
185
-
186
- # def method_missing(m, *args, &ruby_block)
187
- # # print "method_missing in class=#{self.class} with m=#{m}\n"
188
- # # Not a value, but maybe it is in the stack of SW blocks
189
- # SBLOCK_STACK.reverse_each do |sblock|
190
- # if sblock.respond_to?(m) then
191
- # return sblock.send(m,*args,&ruby_block)
192
- # end
193
- # end
194
- # # puts "here: #{m}"
195
- # # No, true error
196
- # self.old_method_missing(m,*args,&ruby_block)
197
- # end
198
- # end
199
-
200
-
201
- # RUBY_OPERATOR = {
202
- # # Unary operators.
203
- # :"-@" => "-(%s)", :"+@" => "+(%s)", :"~" => "~(%s)",
204
- # :abs => "(%s).abs",
205
- # :boolean => "%s", :bit => "%s",
206
- # :signed => "%s", :unsigned => "(%s) & 0xFFFFFFFFFFFFFFFF",
207
-
208
- # # Binary operators.
209
- # :"+" => "(%s)+(%s)", :"-" => "(%s)-(%s)", :"*" => "(%s)*(%s)",
210
- # :"/" => "(%s)/(%s)", :"%" => "(%s)%%(%s)", :"**" => "(%s)**(%s)",
211
- # :"&" => "(%s)&(%s)", :"|" => "(%s)|(%s)", :"^" => "(%s)^(%s)",
212
- # :"<<" => "(%s)<<(%s)", :">>" => "(%s)>>(%s)",
213
- # :"==" => "(%s)==(%s)", :"!=" => "(%s)!=(%s)",
214
- # :"<" => "(%s)<(%s)", :">" => "(%s)>(%s)",
215
- # :"<=" => "(%s)<=(%s)",:">=" => "(%s)>=(%s)"
216
- # }
217
182
 
218
183
  # The translation of operators into Ruby code.
219
184
  RUBY_OPERATOR = {
@@ -238,10 +203,29 @@ module RubyHDL::High
238
203
  :">=" => "((%{l}) & %{m}%{s} >=(%{r}) & %{m}%{s}) ? 1:0"
239
204
  }
240
205
 
206
+ # The translation of operators into C code.
207
+ C_OPERATOR = {
208
+ # Unary operators.
209
+ :"-@" => "-(%s)", :"+@" => "+(%s)", :"~" => "~(%s)",
210
+ :abs => "(%s).abs",
211
+ :boolean => "%s", :bit => "%s",
212
+ :signed => "%s", :unsigned => "(%s) & 0xFFFFFFFFFFFFFFFF",
213
+
214
+ # Binary operators.
215
+ :"+" => "(%s)+(%s)", :"-" => "(%s)-(%s)", :"*" => "(%s)*(%s)",
216
+ :"/" => "(%s)/(%s)", :"%" => "(%s)%%(%s)", :"**" => "pow((%s),(%s))",
217
+ :"&" => "(%s)&(%s)", :"|" => "(%s)|(%s)", :"^" => "(%s)^(%s)",
218
+ :"<<" => "(%s)<<(%s)", :">>" => "(%s)>>(%s)",
219
+ :"==" => "(%s)==(%s)", :"!=" => "(%s)!=(%s)",
220
+ :"<" => "(%s)<(%s)", :">" => "(%s)>(%s)",
221
+ :"<=" => "(%s)<=(%s)",:">=" => "(%s)>=(%s)"
222
+ }
223
+
224
+ # The translation of operators into Python code.
241
225
  PYTHON_OPERATOR = {
242
226
  # Unary operators.
243
227
  :"-@" => "-(%{l})", :"+@" => "+(%{l})", :"~" => "~(%{l})",
244
- :abs => "(%{l}).abs",
228
+ :abs => "abs(%{l})",
245
229
  :boolean => "%{l}", :bit => "%{l}",
246
230
  :signed => "%{l}", :unsigned => "(%{l}) & 0xFFFFFFFFFFFFFFFF",
247
231
 
@@ -260,22 +244,31 @@ module RubyHDL::High
260
244
  :">=" => "1 if ((%{l}) & %{m}%{s} >=(%{r}) & %{m}%{s}) else 0"
261
245
  }
262
246
 
263
- # The translation of operators into C code.
264
- C_OPERATOR = {
247
+ # The translation of operators into TensorFlow code.
248
+ TF_OPERATOR = {
265
249
  # Unary operators.
266
- :"-@" => "-(%s)", :"+@" => "+(%s)", :"~" => "~(%s)",
267
- :abs => "(%s).abs",
268
- :boolean => "%s", :bit => "%s",
269
- :signed => "%s", :unsigned => "(%s) & 0xFFFFFFFFFFFFFFFF",
250
+ :"-@" => "-(%{l})", :"+@" => "+(%{l})",
251
+ :"~" => "tf.bitwise.bitwise_not(%{l})",
252
+ :abs => "tf.abs(%{l})",
253
+ :boolean => "%{l}", :bit => "%{l}",
254
+ :signed => "%{l}",
255
+ :unsigned => "tf.bitwise.bitwise_and(%{l}),0xFFFFFFFFFFFFFFFF",
270
256
 
271
257
  # Binary operators.
272
- :"+" => "(%s)+(%s)", :"-" => "(%s)-(%s)", :"*" => "(%s)*(%s)",
273
- :"/" => "(%s)/(%s)", :"%" => "(%s)%%(%s)", :"**" => "pow((%s),(%s))",
274
- :"&" => "(%s)&(%s)", :"|" => "(%s)|(%s)", :"^" => "(%s)^(%s)",
275
- :"<<" => "(%s)<<(%s)", :">>" => "(%s)>>(%s)",
276
- :"==" => "(%s)==(%s)", :"!=" => "(%s)!=(%s)",
277
- :"<" => "(%s)<(%s)", :">" => "(%s)>(%s)",
278
- :"<=" => "(%s)<=(%s)",:">=" => "(%s)>=(%s)"
258
+ :"+" => "(%{l})+(%{r})", :"-" => "(%{l})-(%{r})",
259
+ :"*" => "(%{l})*(%{r})", :"/" => "(%{l})/(%{r})",
260
+ :"%" => "(%{l})%%(%{r})", :"**" => "(%{l})**(%{r})",
261
+ :"&" => "tf.bitwise.bitwise_and((%{l}),(%{r}))",
262
+ :"|" => "tf.bitwise.bitwise_or((%{l}),(%{r}))",
263
+ :"^" => "tf.bitwise.bitwise_xor((%{l}),(%{r}))",
264
+ :"<<" => "tf.bitwise.bitwise_left_shift((%{l}),(%{r}))",
265
+ :">>" => "tf.bitwise.bitwise_right_shift((%{l}),(%{r}))",
266
+ :"==" => "tf.equal(%{l},%{r})",
267
+ :"!=" => "tf.not_equal(%{l},%{r})",
268
+ :"<" => "tf.less(%{l},%{r})",
269
+ :">" => "tf.greater(%{l},%{r})",
270
+ :"<=" => "tf.less_equal(%{l},%{r})",
271
+ :">=" => "tf.greater_equal(%{l},%{r})"
279
272
  }
280
273
 
281
274
 
@@ -599,6 +592,310 @@ module RubyHDL::High
599
592
  end
600
593
 
601
594
 
595
+ # Module adding functionalities to object including the +heach+ method.
596
+ module HEnumerable
597
+
598
+ # Iterator on each of the elements in range +rng+.
599
+ # *NOTE*:
600
+ # - Stop iteration when the end of the range is reached or when there
601
+ # are no elements left
602
+ # - This is not a method from Ruby but one specific for hardware where
603
+ # creating a array is very expensive.
604
+ def heach_range(rng,&ruby_block)
605
+ self.heach.heach_range(rng,&ruby_block)
606
+ end
607
+
608
+ # Tell if all the elements respect a given criterion given either
609
+ # as +arg+ or as block.
610
+ def hall?(arg = nil,&ruby_block)
611
+ self.heach.hall?(arg,&ruby_block)
612
+ end
613
+
614
+ # Tell if any of the elements respects a given criterion given either
615
+ # as +arg+ or as block.
616
+ def hany?(arg = nil,&ruby_block)
617
+ self.heach.hany?(arg,&ruby_block)
618
+ end
619
+
620
+ # Returns an HEnumerator generated from current enumerable and +arg+
621
+ def hchain(arg)
622
+ self.heach.hchain(arg)
623
+ end
624
+
625
+ # HW implementation of the Ruby chunk.
626
+ # NOTE: to do, or may be not.
627
+ def hchunk(*args,&ruby_block)
628
+ raise "hchunk is not supported yet."
629
+ end
630
+
631
+ # HW implementation of the Ruby chunk_while.
632
+ # NOTE: to do, or may be not.
633
+ def hchunk_while(*args,&ruby_block)
634
+ raise "hchunk_while is not supported yet."
635
+ end
636
+
637
+ # Returns a vector containing the execution result of the given block
638
+ # on each element. If no block is given, return an HEnumerator.
639
+ # NOTE: be carful that the resulting vector can become huge if there
640
+ # are many element.
641
+ def hmap(&ruby_block)
642
+ self.heach.hmap(&ruby_block)
643
+ end
644
+
645
+ # HW implementation of the Ruby flat_map.
646
+ # NOTE: actually due to the way HDLRuby handles vectors, should work
647
+ # like smap
648
+ def hflat_map(&ruby_block)
649
+ self.heach.hflat_map(&ruby_block)
650
+ end
651
+
652
+ # HW implementation of the Ruby compact, but remove 0 values instead
653
+ # on nil (since nil that does not have any meaning in HW).
654
+ def hcompact
655
+ self.heach.hcompact(&ruby_block)
656
+ end
657
+
658
+
659
+ # WH implementation of the Ruby count.
660
+ def hcount(obj = nil, &ruby_block)
661
+ self.heach.hcount(obj,&ruby_block)
662
+ end
663
+
664
+ # HW implementation of the Ruby cycle.
665
+ def hcycle(n = nil,&ruby_block)
666
+ self.heach.hcycle(n,&ruby_block)
667
+ end
668
+
669
+ # HW implementation of the Ruby find.
670
+ # NOTE: contrary to Ruby, if_none_proc is mandatory since there is no
671
+ # nil in HW. Moreover, the argument can also be a value.
672
+ def hfind(if_none_proc, &ruby_block)
673
+ self.seach.hfind(if_non_proc,&ruby_block)
674
+ end
675
+
676
+ # HW implementation of the Ruby drop.
677
+ def hdrop(n)
678
+ self.heach.sdrop(n)
679
+ end
680
+
681
+ # HW implementation of the Ruby drop_while.
682
+ def hdrop_while(&ruby_block)
683
+ self.heach.sdrop_while(n)
684
+ end
685
+
686
+ # HW implementation of the Ruby each_cons
687
+ def heach_cons(n,&ruby_block)
688
+ self.heach.heach_cons(n,&ruby_block)
689
+ end
690
+
691
+ # HW implementation of the Ruby each_entry.
692
+ # NOTE: to do, or may be not.
693
+ def heach_entry(*args,&ruby_block)
694
+ raise "heach_entry is not supported yet."
695
+ end
696
+
697
+ # HW implementation of the Ruby each_slice
698
+ def heach_slice(n,&ruby_block)
699
+ self.heach.heach_slice(n,&ruby_block)
700
+ end
701
+
702
+ # HW implementation of the Ruby each_with_index.
703
+ def heach_with_index(*args,&ruby_block)
704
+ self.heach.hwith_index(*args,&ruby_block)
705
+ end
706
+
707
+ # HW implementation of the Ruby each_with_object.
708
+ def heach_with_object(obj,&ruby_block)
709
+ self.heach.hwith_object(obj,&ruby_block)
710
+ end
711
+
712
+ # HW implementation of the Ruby to_a.
713
+ def hto_a
714
+ self.heach.hto_a
715
+ end
716
+
717
+ # HW implementation of the Ruby select.
718
+ def hselect(&ruby_block)
719
+ self.heach.hselect(&ruby_block)
720
+ end
721
+
722
+ # HW implementation of the Ruby find_index.
723
+ def hfind_index(obj = nil, &ruby_block)
724
+ self.heach.hfind_index(obj,&ruby_block)
725
+ end
726
+
727
+ # HW implementation of the Ruby first.
728
+ def hfirst(n=1)
729
+ self.heach.hfirst(n)
730
+ end
731
+
732
+ # HW implementation of the Ruby grep.
733
+ # NOTE: to do, or may be not.
734
+ def hgrep(*args,&ruby_block)
735
+ raise "hgrep is not supported yet."
736
+ end
737
+
738
+ # HW implementation of the Ruby grep_v.
739
+ # NOTE: to do, or may be not.
740
+ def hgrep_v(*args,&ruby_block)
741
+ raise "hgrep_v is not supported yet."
742
+ end
743
+
744
+ # HW implementation of the Ruby group_by.
745
+ # NOTE: to do, or may be not.
746
+ def hgroup_by(*args,&ruby_block)
747
+ raise "hgroup_by is not supported yet."
748
+ end
749
+
750
+ # HW implementation of the Ruby include?
751
+ def hinclude?(obj)
752
+ return self.heach.hinclude?(obj)
753
+ end
754
+
755
+ # HW implementation of the Ruby inject.
756
+ def hinject(*args,&ruby_block)
757
+ return self.heach.hinject(*args,&ruby_block)
758
+ end
759
+
760
+ # HW implementation of the Ruby reduce.
761
+ def hreduce(*args,&ruby_block)
762
+ return self.heach.hreduce(*args,&ruby_block)
763
+ end
764
+
765
+ # HW implementation of the Ruby lazy.
766
+ # NOTE: to do, or may be not.
767
+ def hlazy(*args,&ruby_block)
768
+ raise "hlazy is not supported yet."
769
+ end
770
+
771
+ # HW implementation of the Ruby max.
772
+ def hmax(n = nil, &ruby_block)
773
+ return self.heach.hmax(n,&ruby_block)
774
+ end
775
+
776
+ # HW implementation of the Ruby max_by.
777
+ def hmax_by(n = nil, &ruby_block)
778
+ return self.heach.hmax_by(n,&ruby_block)
779
+ end
780
+
781
+ # HW implementation of the Ruby min.
782
+ def hmin(n = nil, &ruby_block)
783
+ return self.heach.hmin(n,&ruby_block)
784
+ end
785
+
786
+ # HW implementation of the Ruby min_by.
787
+ def hmin_by(n = nil, &ruby_block)
788
+ return self.heach.hmin_by(n,&ruby_block)
789
+ end
790
+
791
+ # HW implementation of the Ruby minmax.
792
+ def hminmax(&ruby_block)
793
+ return self.heach.hminmax(&ruby_block)
794
+ end
795
+
796
+ # HW implementation of the Ruby minmax_by.
797
+ def hminmax_by(&ruby_block)
798
+ return self.heach.hminmax_by(&ruby_block)
799
+ end
800
+
801
+ # Tell if none of the elements respects a given criterion given either
802
+ # as +arg+ or as block.
803
+ def hnone?(arg = nil,&ruby_block)
804
+ return self.heach.hnone?(arg,&ruby_block)
805
+ end
806
+
807
+ # Tell if one and only one of the elements respects a given criterion
808
+ # given either as +arg+ or as block.
809
+ def hone?(arg = nil,&ruby_block)
810
+ return self.heach.hone?(arg,&ruby_block)
811
+ end
812
+
813
+ # HW implementation of the Ruby partition.
814
+ # NOTE: to do, or may be not.
815
+ def hpartition(*args,&ruby_block)
816
+ raise "hpartition is not supported yet."
817
+ end
818
+
819
+ # HW implementatiob of the Ruby reject.
820
+ def hreject(&ruby_block)
821
+ return self.heach.hreject(&ruby_block)
822
+ end
823
+
824
+ # HW implementatiob of the Ruby reverse_each.
825
+ def hreverse_each(*args,&ruby_block)
826
+ return self.heach.hreverse_each(*args,&ruby_block)
827
+ end
828
+
829
+ # HW implementation of the Ruby slice_after.
830
+ # NOTE: to do, or may be not.
831
+ def hslice_after(pattern = nil,&ruby_block)
832
+ raise "hslice_after is not supported yet."
833
+ end
834
+
835
+ # HW implementation of the Ruby slice_before.
836
+ # NOTE: to do, or may be not.
837
+ def hslice_before(*args,&ruby_block)
838
+ raise "hslice_before is not supported yet."
839
+ end
840
+
841
+ # HW implementation of the Ruby slice_when.
842
+ # NOTE: to do, or may be not.
843
+ def hslice_when(*args,&ruby_block)
844
+ raise "hslice_before is not supported yet."
845
+ end
846
+
847
+ # HW implementation of the Ruby sort.
848
+ def hsort(&ruby_block)
849
+ return self.heach.hsort(&ruby_block)
850
+ end
851
+
852
+ # HW implementation of the Ruby sort.
853
+ def hsort_by(&ruby_block)
854
+ return self.heach.hsort_by(&ruby_block)
855
+ end
856
+
857
+ # HW implementation of the Ruby sum.
858
+ def hsum(initial_value = nil,&ruby_block)
859
+ return self.heach.hsum(initial_value,&ruby_block)
860
+ end
861
+
862
+ # The HW implementation of the Ruby take.
863
+ def htake(n)
864
+ return self.heach.htake(n)
865
+ end
866
+
867
+ # The HW implementation of the Ruby take_while.
868
+ def htake_while(&ruby_block)
869
+ return self.heach.htake_while(&ruby_block)
870
+ end
871
+
872
+ # HW implementation of the Ruby tally.
873
+ # NOTE: to do, or may be not.
874
+ def htally(h = nil)
875
+ raise "htally is not supported yet."
876
+ end
877
+
878
+ # HW implementation of the Ruby to_h.
879
+ # NOTE: to do, or may be not.
880
+ def hto_h(h = nil)
881
+ raise "hto_h is not supported yet."
882
+ end
883
+
884
+ # HW implementation of the Ruby uniq.
885
+ def huniq(&ruby_block)
886
+ return self.heach.huniq(&ruby_block)
887
+ end
888
+
889
+ # HW implementation of the Ruby zip.
890
+ # NOTE: for now szip is deactivated untile tuples are properly
891
+ # handled by HDLRuby.
892
+ def hzip(obj,&ruby_block)
893
+ return self.heach.hzip(obj,&ruby_block)
894
+ end
895
+
896
+ end
897
+
898
+
602
899
  # Modify String to act as Ruby code generator.
603
900
  refine ::String do
604
901
  # Convert to Ruby code.
@@ -613,6 +910,11 @@ module RubyHDL::High
613
910
  def to_python(l = "")
614
911
  to_ruby
615
912
  end
913
+
914
+ # Convert to TensorFlow
915
+ def to_tf(l = "")
916
+ to_python
917
+ end
616
918
  end
617
919
 
618
920
 
@@ -640,6 +942,10 @@ module RubyHDL::High
640
942
  return self.to_s
641
943
  end
642
944
 
945
+ def to_tf(l = "")
946
+ return self.to_s
947
+ end
948
+
643
949
  # Enhance the Integer class with sequencer iterations.
644
950
 
645
951
  # HW times iteration.
@@ -647,15 +953,30 @@ module RubyHDL::High
647
953
  self.to_value.stimes(&ruby_block)
648
954
  end
649
955
 
956
+ # HW times iteration. (parallel version)
957
+ def htimes(&ruby_block)
958
+ self.to_value.htimes(&ruby_block)
959
+ end
960
+
650
961
  # HW upto iteration.
651
962
  def supto(val,&ruby_block)
652
963
  self.to_value.supto(&ruby_block)
653
964
  end
654
965
 
966
+ # HW upto iteration. (parallel version)
967
+ def hupto(val,&ruby_block)
968
+ self.to_value.hupto(&ruby_block)
969
+ end
970
+
655
971
  # HW downto iteration.
656
972
  def sdownto(val,&ruby_block)
657
973
  self.to_value.sdownto(&ruby_block)
658
974
  end
975
+
976
+ # HW downto iteration.
977
+ def hdownto(val,&ruby_block)
978
+ self.to_value.hdownto(&ruby_block)
979
+ end
659
980
  end
660
981
 
661
982
  # Modify Float to act as value.
@@ -675,15 +996,74 @@ module RubyHDL::High
675
996
  def to_python(l = "")
676
997
  return "range(#{self.first.to_python}, #{self.last.to_python} + 1)"
677
998
  end
999
+
1000
+ def to_tf(l = "")
1001
+ return "tf.range(#{self.first.to_python}, #{self.last.to_python} + 1)"
1002
+ end
678
1003
  end
679
1004
 
680
1005
  # Modify Range to support HW iterators.
681
1006
  refine ::Enumerable do
682
1007
  import_methods SEnumerable
1008
+
683
1009
  # HW iteration on each element.
684
1010
  def seach(&ruby_block)
685
1011
  return Siter.new(RubyHDL::High.top_sblock.sequencer,self,"each",&ruby_block)
686
1012
  end
1013
+
1014
+ # HW iterations. (parallel version)
1015
+ alias_method :hall?, :all?
1016
+ alias_method :hany?, :any?
1017
+ alias_method :hchain, :chain
1018
+ alias_method :hchunk, :chunk
1019
+ alias_method :hchunk_while, :chunk_while
1020
+ alias_method :hmap, :map
1021
+ alias_method :hflat_map, :flat_map
1022
+ alias_method :hcompact, :compact
1023
+ alias_method :hcount, :count
1024
+ alias_method :hcycle, :cycle
1025
+ alias_method :hfind, :find
1026
+ alias_method :hdrop, :drop
1027
+ alias_method :hdrop_while, :drop_while
1028
+ alias_method :heach_cons, :each_cons
1029
+ alias_method :heach_entry, :each_entry
1030
+ alias_method :heach_slice, :each_slice
1031
+ alias_method :heach_with_index, :each_with_index
1032
+ alias_method :heach_with_object, :each_with_object
1033
+ alias_method :hto_a, :to_a
1034
+ alias_method :hselect, :select
1035
+ alias_method :hfind_index, :find_index
1036
+ alias_method :hfirst, :first
1037
+ alias_method :hgrep, :grep
1038
+ alias_method :hgrep_v, :grep_v
1039
+ alias_method :hgroup_by, :group_by
1040
+ alias_method :hinclude?, :include?
1041
+ alias_method :hinject, :inject
1042
+ alias_method :hreduce, :reduce
1043
+ alias_method :hlazy, :lazy
1044
+ alias_method :hmax, :max
1045
+ alias_method :hmax_by, :max_by
1046
+ alias_method :hmin, :min
1047
+ alias_method :hmin_by, :min_by
1048
+ alias_method :hminmax, :minmax
1049
+ alias_method :hminmax_by, :minmax_by
1050
+ alias_method :hnone?, :none?
1051
+ alias_method :hone?, :one?
1052
+ alias_method :hpartition, :partition
1053
+ alias_method :hreject, :reject
1054
+ alias_method :hreverse_each,:reverse_each
1055
+ alias_method :hslice_after, :slice_after
1056
+ alias_method :hslice_before,:slice_before
1057
+ alias_method :hslice_when, :slice_when
1058
+ alias_method :hsort, :sort
1059
+ alias_method :hsort_by, :sort_by
1060
+ alias_method :hsum, :sum
1061
+ alias_method :htake, :take
1062
+ alias_method :htake_while, :take_while
1063
+ alias_method :htally, :tally
1064
+ alias_method :hto_h, :to_h
1065
+ alias_method :huniq, :uniq
1066
+ alias_method :hzip, :zip
687
1067
  end
688
1068
 
689
1069
 
@@ -728,6 +1108,9 @@ module RubyHDL::High
728
1108
  # Generate the resulting signals.
729
1109
  return type.inner(*names)
730
1110
  end
1111
+
1112
+ # HW iteration on each element. (parallel version)
1113
+ alias_method :heach, :each
731
1114
  end
732
1115
 
733
1116
 
@@ -1099,6 +1482,11 @@ module RubyHDL::High
1099
1482
  # By default: 0
1100
1483
  return "0"
1101
1484
  end
1485
+
1486
+ # Convert to tensorflow initialization code.
1487
+ def to_tf_init
1488
+ return "tf.constant(0)"
1489
+ end
1102
1490
  end
1103
1491
 
1104
1492
 
@@ -1395,6 +1783,18 @@ module RubyHDL::High
1395
1783
  return "0"
1396
1784
  end
1397
1785
  end
1786
+
1787
+ # Convert to tensorflow initialization code.
1788
+ def to_tf_init
1789
+ if @base.is_a?(TypeVector) then
1790
+ # Array type case.
1791
+ base_init = @base.to_python_init
1792
+ return "tf.constant([" +
1793
+ ([base_init] * self.size.to_i).join(",") + "])"
1794
+ else
1795
+ return "tf.constant(0)"
1796
+ end
1797
+ end
1398
1798
  end
1399
1799
 
1400
1800
 
@@ -1791,6 +2191,8 @@ module RubyHDL::High
1791
2191
  class Expression
1792
2192
  using RubyHDL::High
1793
2193
 
2194
+ include HEnumerable
2195
+
1794
2196
  attr_reader :type
1795
2197
  # Create a new expression with +type+ data type.
1796
2198
  def initialize(type)
@@ -1824,6 +2226,11 @@ module RubyHDL::High
1824
2226
  raise "to_python not defined for class: #{self.class}."
1825
2227
  end
1826
2228
 
2229
+ # Convert to TensorFlow code.
2230
+ def to_tf(l = "")
2231
+ raise "to_python not defined for class: #{self.class}."
2232
+ end
2233
+
1827
2234
  # Convert to ruby code for left value.
1828
2235
  # By default: the same as to_ruby
1829
2236
  alias_method :to_ruby_left, :to_ruby
@@ -1950,6 +2357,14 @@ module RubyHDL::High
1950
2357
  RubyHDL::High.top_sblock <<
1951
2358
  Siter.new(RubyHDL::High.top_sblock.sequencer,self,"downto",&ruby_block)
1952
2359
  end
2360
+
2361
+ # HW iteration on each element. (parallel version)
2362
+ def heach(&ruby_block)
2363
+ return self unless ruby_block
2364
+ self.type.width.each do |i|
2365
+ ruby_block.call(self[i])
2366
+ end
2367
+ end
1953
2368
  end
1954
2369
 
1955
2370
 
@@ -1970,6 +2385,15 @@ module RubyHDL::High
1970
2385
  return self
1971
2386
  end
1972
2387
 
2388
+ # Convert to an integer.
2389
+ def to_i
2390
+ if @content.is_a?(::Array) then
2391
+ raise "Cannot convert an array Value to an integer."
2392
+ else
2393
+ return @content.to_i
2394
+ end
2395
+ end
2396
+
1973
2397
  # Convert to Ruby code.
1974
2398
  def to_ruby
1975
2399
  return @content.to_s
@@ -1988,6 +2412,12 @@ module RubyHDL::High
1988
2412
  def to_python(l = "")
1989
2413
  return @content.to_s
1990
2414
  end
2415
+
2416
+ # Convert to TensorFlow code.
2417
+ def to_tf(l = "")
2418
+ return @content.to_s
2419
+ end
2420
+
1991
2421
  end
1992
2422
 
1993
2423
 
@@ -2017,6 +2447,11 @@ module RubyHDL::High
2017
2447
  def to_python(l = "")
2018
2448
  return PYTHON_OPERATOR[@operator] % { l: @child.to_python }
2019
2449
  end
2450
+
2451
+ # Convert to Python code.
2452
+ def to_tf(l = "")
2453
+ return TF_OPERATOR[@operator] % { l: @child.to_tf }
2454
+ end
2020
2455
  end
2021
2456
 
2022
2457
  # Describes the software implementation of an binary operation.
@@ -2057,6 +2492,12 @@ module RubyHDL::High
2057
2492
  return PYTHON_OPERATOR[@operator] %
2058
2493
  { l: @left.to_python, r: @right.to_python, m: @mask, s: @sign_fix }
2059
2494
  end
2495
+
2496
+ # Convert to TensorFlow code.
2497
+ def to_tf(l = "")
2498
+ return TF_OPERATOR[@operator] %
2499
+ { l: @left.to_tf, r: @right.to_tf, m: @mask, s: @sign_fix }
2500
+ end
2060
2501
  end
2061
2502
 
2062
2503
  # Describes the software implementation of an select operation.
@@ -2071,10 +2512,11 @@ module RubyHDL::High
2071
2512
 
2072
2513
  # Convert to Ruby code.
2073
2514
  def to_ruby
2074
- return @sequencer.clk_up + "\ncase(#{@sel.to_ruby}) ; " +
2075
- @choices.map.with_index do |choice,i|
2076
- "when #{i} ; #{choice.to_ruby} ; "
2077
- end.join + "end\n" + @sequencer.clk_up
2515
+ # return "\ncase(#{@sel.to_ruby}) ; " +
2516
+ # @choices.map.with_index do |choice,i|
2517
+ # "when #{i} ; #{choice.to_ruby} ; "
2518
+ # end.join + "end\n"
2519
+ return "[" + @choices.map(&:to_ruby) + "][#{@sel.to_ruby}]"
2078
2520
  end
2079
2521
 
2080
2522
  # Convert to C code.
@@ -2083,18 +2525,31 @@ module RubyHDL::High
2083
2525
  # @choices.map.with_index do |choice,i|
2084
2526
  # "case #{i}:\n#{choice.to_c}\nbreak;"
2085
2527
  # end.join("\n") + "\n}"
2086
- return @sequencer.clk_up_c +
2087
- "\n#{@sel.to_c} ? #{@choices[1].to_c} : #{@choices[0].to_c}" +
2088
- @sequencer.clk_up_c
2528
+ selc = @sel.to_c
2529
+ return @choices.map.with_index do |choice, i|
2530
+ "#{selc} == #{i} ? #{choice} : "
2531
+ end.join + "0"
2089
2532
  end
2090
2533
 
2091
2534
  # Convert to Python code.
2092
2535
  def to_python(l = "")
2093
- return @sequencer.clk_up_python(l) +
2094
- "\n#{l}match #{@sel.to_python}:\n" +
2095
- @choices.map.with_index do |choice,i|
2096
- "#{l} case #{i}:\n #{choice.to_python(l + " ")}\n"
2097
- end.join + @sequencer.clk_up_pyhton(l)
2536
+ # return "\n#{l}match #{@sel.to_python}:\n" +
2537
+ # @choices.map.with_index do |choice,i|
2538
+ # "#{l} case #{i}:\n #{choice.to_python(l + " ")}\n"
2539
+ # end.join
2540
+ return "[" + @choices.map(&:to_python) + "][#{@sel.to_python}]"
2541
+ end
2542
+
2543
+ # Convert to TensorFlow code.
2544
+ def to_tf(l = "")
2545
+ # return "tf.switch_case(tf.cast(#{@sel.to_tf},tf.int32)," +
2546
+ # "\nbranch_fns={" +
2547
+ # @choices.map.with_index do |choice,i|
2548
+ # (i < @choices.size-1) ?
2549
+ # "\n#{i}: lambda: #{choice.to_tf(l + " ")}," :
2550
+ # "\n#{i}: lambda: #{choice.to_tf(l + " ")}},"
2551
+ # end.join + ")"
2552
+ return "tf.constant([" + @choices.map(&:to_python) + "])[#{@sel.to_python}]"
2098
2553
  end
2099
2554
  end
2100
2555
 
@@ -2140,6 +2595,11 @@ module RubyHDL::High
2140
2595
  def to_python(l = "")
2141
2596
  return to_ruby
2142
2597
  end
2598
+
2599
+ # Convert to TensorFlow code.
2600
+ def to_tf(l = "")
2601
+ return to_tf
2602
+ end
2143
2603
  end
2144
2604
 
2145
2605
  # Describes a SW implementation of an index reference.
@@ -2152,6 +2612,7 @@ module RubyHDL::High
2152
2612
  super(type)
2153
2613
  @base = base.to_expr
2154
2614
  @idx = idx.to_expr
2615
+ @npow = 2 ** @idx.type.width
2155
2616
  end
2156
2617
 
2157
2618
  # Get the final base object of the binary if it is an [] operator.
@@ -2197,17 +2658,22 @@ module RubyHDL::High
2197
2658
 
2198
2659
  # Convert to Ruby code.
2199
2660
  def to_ruby
2200
- return "#{@base.to_ruby}[#{@idx.to_ruby}]"
2661
+ return "#{@base.to_ruby}[(#{@idx.to_ruby}) % #{@npow}]"
2201
2662
  end
2202
2663
 
2203
2664
  # Convert to C code.
2204
2665
  def to_c
2205
- return "#{@base.to_c}[#{@idx.to_c}]"
2666
+ return "#{@base.to_c}[(#{@idx.to_c}) % #{@npow}]"
2206
2667
  end
2207
2668
 
2208
2669
  # Convert to Python code.
2209
2670
  def to_python(l = "")
2210
- return "#{@base.to_python}[#{@idx.to_python}]"
2671
+ return "#{@base.to_python}[(#{@idx.to_python}) % #{@npow}]"
2672
+ end
2673
+
2674
+ # Convert to TensorFlow code.
2675
+ def to_tf(l = "")
2676
+ return "#{@base.to_tf}[#{@idx.to_tf} % #{@npow}]"
2211
2677
  end
2212
2678
  end
2213
2679
 
@@ -2292,7 +2758,12 @@ module RubyHDL::High
2292
2758
 
2293
2759
  # Convert to Python code.
2294
2760
  def to_python(l = "")
2295
- return "#{@base.to_python}[#{@rng.last.to_python}:#{@rng.first.to_python}]"
2761
+ return "#{@base.to_python}[#{@rng.last.to_python}:#{(@rng.first+1).to_python}]"
2762
+ end
2763
+
2764
+ # Convert to TensorFlow code.
2765
+ def to_tf(l = "")
2766
+ return "#{@base.to_tf}[#{@rng.last.to_tf}:#{(@rng.first+1).to_tf}]"
2296
2767
  end
2297
2768
  end
2298
2769
 
@@ -2410,8 +2881,36 @@ module RubyHDL::High
2410
2881
  return "#{l}#{@left.to_python} = #{@right.to_python}"
2411
2882
  end
2412
2883
  end
2884
+
2885
+ # Convert to TensorFlow code.
2886
+ def to_tf(l = "")
2887
+ righttf = @right.to_tf
2888
+ # Right value must be a tf object.
2889
+ righttf = "tf.constant(#{righttf})" if righttf.to_i.to_s == righttf
2890
+ # Generate the transmit.
2891
+ if (@left.is_a?(RefIndex) or @left.is_a?(RefRange)) then
2892
+ if @left.base.type.base.is_a?(TypeVector) then
2893
+ # Assign inside array.
2894
+ return "#{l}#{@left.to_tf} = #{righttf}"
2895
+ else
2896
+ # Get the access range.
2897
+ rng = @left.range
2898
+ # Compute the writing and clearing masks
2899
+ smask = (1.to_value<<(rng.first+1-rng.last))-1
2900
+ cmask = ~(smask << rng.last)
2901
+ # Get the final base.
2902
+ base = left.final_base.to_ruby
2903
+ # Generate the ruby code.
2904
+ return "#{l}#{base} = tf.bitwise.bitwise_and(#{base},#{cmask.to_tf})\n" +
2905
+ "#{l}#{base} = tf.bitwise.bitwise_or(#{base},(tf.bitwise.bitwise_left_shift(tf.bitwise.bitwise_and(#{righttf},#{smask.to_tf}),(#{rng.last.to_tf}))))"
2906
+ end
2907
+ else
2908
+ return "#{l}#{@left.to_tf} = #{righttf}"
2909
+ end
2910
+ end
2413
2911
  end
2414
2912
 
2913
+
2415
2914
  # Describes a SW implementation of a sif statement.
2416
2915
  class Sif < Statement
2417
2916
  # Create a new if statement in sequencer +sequencer+
@@ -2486,7 +2985,7 @@ module RubyHDL::High
2486
2985
  # Convert to Python code.
2487
2986
  def to_python(l = "")
2488
2987
  res = @sequencer.clk_up_python(l) +
2489
- "\n#{l}if (#{@condition.to_ruby}) != 0:\n" +
2988
+ "\n#{l}if (#{@condition.to_python}) != 0:\n" +
2490
2989
  "#{@yes_blk.to_python(l + " ")}\n"
2491
2990
  @elsifs.each do |(cond,blk)|
2492
2991
  res << "#{l}elif (#{cond.to_python}) != 0:\n" +
@@ -2497,6 +2996,21 @@ module RubyHDL::High
2497
2996
  end
2498
2997
  return res + @sequencer.clk_up_python(l)
2499
2998
  end
2999
+
3000
+ # Convert to TensorFlow code.
3001
+ def to_tf(l = "")
3002
+ res = "\n#{l}tf.cond(#{@condition.to_tf},\n" +
3003
+ "\n#{l} lambda: #{@yes_blk.to_f(l + " ")},\n"
3004
+ @elseif.each do |(cond,blk)|
3005
+ res<<"\n#{l} lambda: tf.cond(#{cond.to_tf}, lambda: #{blk}),\n"
3006
+ end
3007
+ if @else_blk then
3008
+ res << "\n#{l} lambda: #{else_blk.to_tf(l + " ")})\n"
3009
+ else
3010
+ res << "\n#{l} lambda: tf.constant0)\n"
3011
+ end
3012
+ return res
3013
+ end
2500
3014
  end
2501
3015
 
2502
3016
  # Describes a SW implementation of a hif statement.
@@ -2538,6 +3052,21 @@ module RubyHDL::High
2538
3052
  end
2539
3053
  return res
2540
3054
  end
3055
+
3056
+ # Convert to TensorFlow code.
3057
+ def to_tf(l = "")
3058
+ res = "\n#{l}tf.cond(#{@condition.to_tf},\n" +
3059
+ "\n#{l} lambda: #{@yes_blk.to_f(l + " ")},\n"
3060
+ @elseif.each do |(cond,blk)|
3061
+ res<<"\n#{l} lambda: tf.cond(#{cond.to_tf}, lambda: #{blk}),\n"
3062
+ end
3063
+ if @else_blk then
3064
+ res << "\n#{l} lambda: #{else_blk.to_tf(l + " ")})\n"
3065
+ else
3066
+ res << "\n#{l} lambda: tf.constant0)\n"
3067
+ end
3068
+ return res
3069
+ end
2541
3070
  end
2542
3071
 
2543
3072
  # Describes a SW implementation of a loop statement.
@@ -2580,6 +3109,12 @@ module RubyHDL::High
2580
3109
  return "#{l}while True:\n#{@blk.to_python(l + " ")}\n" +
2581
3110
  @sequencer.clk_up_python(l + " ")
2582
3111
  end
3112
+
3113
+ # Convert to TensorFlow code.
3114
+ def to_tf(l = "")
3115
+ return "#{l}tf.while_loop(lambda: tf.constant(0)," +
3116
+ "_: tf.constant(0) == 0, #{@blk.to_tf(l + " ")})"
3117
+ end
2583
3118
  end
2584
3119
 
2585
3120
  # Describes a SW implementation of a while statement.
@@ -2627,6 +3162,12 @@ module RubyHDL::High
2627
3162
  "#{@yes_blk.to_python(l + " ")}\n" +
2628
3163
  @sequencer.clk_up_python(l + " ")
2629
3164
  end
3165
+
3166
+ # Convert to Tensorflow code.
3167
+ def to_tf(l = "")
3168
+ return "#{l}tf.while_loop(lambda: tf.constant(0)," +
3169
+ "_: #{@condition.to_tf} == 0, #{@yes_blk.to_tf(l + " ")})"
3170
+ end
2630
3171
  end
2631
3172
 
2632
3173
  # Describes a SW implementation of a step statement.
@@ -2650,6 +3191,11 @@ module RubyHDL::High
2650
3191
  def to_python(l = "")
2651
3192
  return @sequencer.clk_up_python(l)
2652
3193
  end
3194
+
3195
+ # Convert to TensorFlow code.
3196
+ def to_tf(l = "")
3197
+ return ""
3198
+ end
2653
3199
  end
2654
3200
 
2655
3201
  # Describes a SW implementation of a break statement.
@@ -2671,7 +3217,12 @@ module RubyHDL::High
2671
3217
 
2672
3218
  # Convert to Python code.
2673
3219
  def to_python(l = "")
2674
- return @sequencer.clk_up_python(l) + "\n#{l}brea;"
3220
+ return @sequencer.clk_up_python(l) + "\n#{l}break;"
3221
+ end
3222
+
3223
+ # Convert to Tensorflow code.
3224
+ def to_tf(l = "")
3225
+ return "return tf.constant(0)"
2675
3226
  end
2676
3227
  end
2677
3228
 
@@ -2696,6 +3247,11 @@ module RubyHDL::High
2696
3247
  def to_python(l = "")
2697
3248
  return @sequencer.clk_up_python(l) + "\n#{l}continue"
2698
3249
  end
3250
+
3251
+ # Convert to TensorFlow code.
3252
+ def to_tf(l = "")
3253
+ raise "continue not supported in TensorFlow yet."
3254
+ end
2699
3255
  end
2700
3256
 
2701
3257
  # Describes a SW implementation of a return statement.
@@ -2721,7 +3277,12 @@ module RubyHDL::High
2721
3277
 
2722
3278
  # Convert to Python code.
2723
3279
  def to_python(l = "")
2724
- return @sequencer.clk_up_python(l) + "\n#{l}return #{@value.to_c}"
3280
+ return @sequencer.clk_up_python(l) + "\n#{l}return #{@value.to_python}"
3281
+ end
3282
+
3283
+ # Convert to TensorFlow code.
3284
+ def to_tf(l = "")
3285
+ return "\n#{l}return #{@value.to_tf}"
2725
3286
  end
2726
3287
  end
2727
3288
 
@@ -2748,6 +3309,11 @@ module RubyHDL::High
2748
3309
  def to_python(l = "")
2749
3310
  return @sequencer.clk_up_python(l) + "\n#{l}return;"
2750
3311
  end
3312
+
3313
+ # Convert to TensorFlow code.
3314
+ def to_tf(l = "")
3315
+ return "\n#{l}return;"
3316
+ end
2751
3317
  end
2752
3318
 
2753
3319
  # Describes a SW synchronization of a signal.
@@ -2781,6 +3347,11 @@ module RubyHDL::High
2781
3347
  def to_python(l = "")
2782
3348
  return "#{l}yield()"
2783
3349
  end
3350
+
3351
+ # Convert to Tensorflow code.
3352
+ def to_tf(l = "")
3353
+ return "#{l}yield()"
3354
+ end
2784
3355
  end
2785
3356
 
2786
3357
  # Describes arbitrary code.
@@ -2848,6 +3419,11 @@ module RubyHDL::High
2848
3419
  def to_python(l = "")
2849
3420
  raise "Ruby objects cannot be converted to Python yet."
2850
3421
  end
3422
+
3423
+ # Convert to TensorFlow code.
3424
+ def to_tf(l = "")
3425
+ raise "Ruby objects cannot be converted to Python yet."
3426
+ end
2851
3427
  end
2852
3428
 
2853
3429
 
@@ -2892,6 +3468,11 @@ module RubyHDL::High
2892
3468
  return "#{l}\n__#{@name}(" + @args.map {|arg| arg.to_python}.join(",") + ")"
2893
3469
  end
2894
3470
 
3471
+ # Convert to TensorFlow code.
3472
+ def to_tf(l = "")
3473
+ return "#{l}\n__#{@name}(" + @args.map {|arg| arg.to_tf}.join(",") + ")"
3474
+ end
3475
+
2895
3476
  # Create an iterator for a given method +meth+.
2896
3477
  def make_iterator(meth,*args,&ruby_block)
2897
3478
  if ruby_block then
@@ -3289,6 +3870,23 @@ module RubyHDL::High
3289
3870
  res << "\n"
3290
3871
  return res
3291
3872
  end
3873
+
3874
+ # Convert to Python code.
3875
+ def to_python(l = "")
3876
+ return "" if @arguments.empty?
3877
+ res = "#{l}print("
3878
+ @arguments.each do |arg|
3879
+ if arg.is_a?(::String) then
3880
+ res << "\"#{arg}\""
3881
+ else
3882
+ res << arg.to_tf
3883
+ end
3884
+ res << ","
3885
+ end
3886
+ res[-1] = ")"
3887
+ res << "\n"
3888
+ return res
3889
+ end
3292
3890
  end
3293
3891
 
3294
3892
 
@@ -3357,6 +3955,23 @@ module RubyHDL::High
3357
3955
  return res + "(#{@blk.to_python})"
3358
3956
  end
3359
3957
 
3958
+ # Convert to TensorFlow code.
3959
+ def to_tf(l = "")
3960
+ if @commands[1] == "times" then
3961
+ # Simple times loop case.
3962
+ if @blk.each_arg.none? then
3963
+ raise "Require an index for loop conversion to TensorFlow."
3964
+ end
3965
+ arg = @blk.each_arg.to_a[0]
3966
+ val = @commands[0]
3967
+ return "#{l}#{arg.to_tf} = tf.range(0.0,#{val.to_tf},1.0)\n" +
3968
+ @blk.to_tf(l)
3969
+ else
3970
+ res = @commands.map { |command| command.to_tf }.join("_")
3971
+ return res + "(#{@blk.to_tf})"
3972
+ end
3973
+ end
3974
+
3360
3975
  # Create an iterator for a given method +meth+.
3361
3976
  def make_iterator(meth,*args,&ruby_block)
3362
3977
  # if ruby_block then
@@ -3696,6 +4311,7 @@ module RubyHDL::High
3696
4311
  end
3697
4312
 
3698
4313
 
4314
+
3699
4315
  # Describes a SW implementation of a signal.
3700
4316
  class SignalI < Expression
3701
4317
  using RubyHDL::High
@@ -3741,6 +4357,11 @@ module RubyHDL::High
3741
4357
  return "__" + self.name.to_s
3742
4358
  end
3743
4359
 
4360
+ # Convert to Tensorflow code.
4361
+ def to_tf(l = "")
4362
+ return "__" + self.name.to_s
4363
+ end
4364
+
3744
4365
  # Check if a value is defined for the signal.
3745
4366
  def value?
3746
4367
  if global? then
@@ -3917,6 +4538,16 @@ module RubyHDL::High
3917
4538
  return res
3918
4539
  end
3919
4540
 
4541
+ # Convert to TensorFlow code.
4542
+ def to_tf(l = "")
4543
+ res = ""
4544
+ # Generate the statements.
4545
+ res += @statements.map do |stmnt|
4546
+ stmnt.to_tf(l) + "\n"
4547
+ end.join
4548
+ return res
4549
+ end
4550
+
3920
4551
  # The interface for describing statements and expressions.
3921
4552
 
3922
4553
  # Mark a step.
@@ -4024,9 +4655,72 @@ module RubyHDL::High
4024
4655
  self << RubyHDL::High::Ruby.new(str,&ruby_block)
4025
4656
  end
4026
4657
 
4027
- # Some arbitrary code whose text is to add direction.
4028
- def text(str)
4029
- self << str.to_s
4658
+ # Some arbitrary text statement without any processing.
4659
+ def text(str,*args)
4660
+ self << RubyHDL::High::VerbatimStatement.new(str,*args)
4661
+ end
4662
+
4663
+ # Some arbitrary expression code whose text is to add direction.
4664
+ def expression(type,str,*args)
4665
+ RubyHDL::High::VerbatimExpression.new(type,str,*args)
4666
+ end
4667
+ end
4668
+
4669
+
4670
+
4671
+ # Describe some statement text code to paste as is in the result.
4672
+ class VerbatimStatement < Statement
4673
+ using RubyHDL::High
4674
+
4675
+ # Create a new verbatim with +str+ of type +type+ as main text and
4676
+ # +args+ as arguments for the text.
4677
+ def initialize(str,*args)
4678
+ @str = str.to_s
4679
+ @args = args
4680
+ end
4681
+
4682
+ # Convert to Ruby code.
4683
+ def to_ruby
4684
+ return @str % @args.map(&:to_ruby)
4685
+ end
4686
+
4687
+ # Convert to Python code.
4688
+ def to_python(l = "")
4689
+ return "#{l}" + @str % @args.map(&:to_python)
4690
+ end
4691
+
4692
+ # Convert to TensorFlow code.
4693
+ def to_tf(l = "")
4694
+ return "#{l}" + @str % @args.map(&:to_tf)
4695
+ end
4696
+ end
4697
+
4698
+
4699
+ # Describe some expression text code to paste as is in the result.
4700
+ class VerbatimExpression < Expression
4701
+ using RubyHDL::High
4702
+
4703
+ # Create a new verbatim with +str+ of type +type+ as main text and
4704
+ # +args+ as arguments for the text.
4705
+ def initialize(type,str,*args)
4706
+ @type = type.to_type
4707
+ @str = str.to_s
4708
+ @args = args
4709
+ end
4710
+
4711
+ # Convert to Ruby code.
4712
+ def to_ruby
4713
+ return @str % @args.map(&:to_ruby)
4714
+ end
4715
+
4716
+ # Convert to Python code.
4717
+ def to_python(l = "")
4718
+ return "#{l}" + @str % @args.map(&:to_python)
4719
+ end
4720
+
4721
+ # Convert to TensorFlow code.
4722
+ def to_tf(l = "")
4723
+ return "#{l}" + @str % @args.map(&:to_tf)
4030
4724
  end
4031
4725
  end
4032
4726
 
@@ -4070,6 +4764,12 @@ module RubyHDL::High
4070
4764
  def to_python(l = "")
4071
4765
  return "#{l}def __#{name}(#{@args.map {|arg| "__" + arg.to_ruby}.join(",")}):\n#{@blk.sequencer.clk_python(l + " ")}\n#{@blk.to_python(l + " ")}\n"
4072
4766
  end
4767
+
4768
+ # Convert to Python code.
4769
+ def to_tf(l = "")
4770
+ return "#{l}@tf.function\n" +
4771
+ "#{l}def __#{name}(#{@args.map {|arg| "__" + arg.to_ruby}.join(",")}):\n#{@blk.to_tf(l + " ")}\n"
4772
+ end
4073
4773
  end
4074
4774
 
4075
4775
 
@@ -4077,6 +4777,7 @@ module RubyHDL::High
4077
4777
 
4078
4778
  # Describes a SW implementation of a sequencer.
4079
4779
  class SequencerT
4780
+ using RubyHDL::High
4080
4781
 
4081
4782
  # The source code (in ruby).
4082
4783
  attr_reader :source
@@ -4145,7 +4846,7 @@ Fiber.new do
4145
4846
  end
4146
4847
  BUILD
4147
4848
  # puts "building code_txt=" + @source
4148
- self.reset!
4849
+ # self.reset!
4149
4850
  end
4150
4851
 
4151
4852
  # Get the Ruby code.
@@ -4221,6 +4922,44 @@ BUILDPYTHON
4221
4922
  end
4222
4923
 
4223
4924
 
4925
+ # Convert to TensorFlow code.
4926
+ def to_tf(l = "")
4927
+ typ = nil
4928
+ res = <<-BUILDTF
4929
+ #{RubyHDL::High.global_sblock.each_signal.map do |signal|
4930
+ typ = signal.type
4931
+ if signal.value? then
4932
+ if signal.array? then
4933
+ res = signal.to_tf + "= [0] * #{signal.type.range.size}"
4934
+ signal.value.each_with_index do |v,i|
4935
+ res += "\n" + signal.to_tf + "[#{i}]=#{v.to_tf}"
4936
+ end
4937
+ res
4938
+ else
4939
+ signal.to_tf + "=" + signal.value.inspect
4940
+ end
4941
+ else
4942
+ if signal.array? then
4943
+ signal.to_tf + " = " + typ.to_tf_init
4944
+ else
4945
+ signal.to_tf + "=" + typ.to_tf_init
4946
+ end
4947
+ end
4948
+ end.join("\n")}
4949
+
4950
+ #{@sfunctions.map {|n,f| f.to_c }.join("\n\n")}
4951
+
4952
+ @tf.function
4953
+ def sequencer():
4954
+ #{RubyHDL::High.global_sblock.each_signal.map do |signal|
4955
+ " global #{signal.to_tf}"
4956
+ end.join("\n")}
4957
+ #{@blk.to_tf(" ")}
4958
+ BUILDTF
4959
+ return res
4960
+ end
4961
+
4962
+
4224
4963
 
4225
4964
 
4226
4965
 
@@ -4258,6 +4997,7 @@ BUILDPYTHON
4258
4997
 
4259
4998
  # Executes the sequencer.
4260
4999
  def resume
5000
+ reset! unless @code
4261
5001
  # @code.call
4262
5002
  @code.resume
4263
5003
  end