HDLRuby 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +5 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +4 -0
  6. data/HDLRuby.gemspec +36 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +2774 -0
  9. data/README.pdf +0 -0
  10. data/Rakefile +10 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/exe/hdrcc +3 -0
  14. data/lib/HDLRuby/alcc.rb +137 -0
  15. data/lib/HDLRuby/backend/hruby_allocator.rb +69 -0
  16. data/lib/HDLRuby/backend/hruby_c_allocator.rb +76 -0
  17. data/lib/HDLRuby/hdr_samples/adder.rb +7 -0
  18. data/lib/HDLRuby/hdr_samples/adder_assign_error.rb +11 -0
  19. data/lib/HDLRuby/hdr_samples/adder_bench.rb +27 -0
  20. data/lib/HDLRuby/hdr_samples/adder_gen.rb +7 -0
  21. data/lib/HDLRuby/hdr_samples/adder_nodef_error.rb +7 -0
  22. data/lib/HDLRuby/hdr_samples/addsub.rb +19 -0
  23. data/lib/HDLRuby/hdr_samples/addsubz.rb +22 -0
  24. data/lib/HDLRuby/hdr_samples/alu.rb +47 -0
  25. data/lib/HDLRuby/hdr_samples/calculator.rb +48 -0
  26. data/lib/HDLRuby/hdr_samples/counter_bench.rb +83 -0
  27. data/lib/HDLRuby/hdr_samples/dff.rb +9 -0
  28. data/lib/HDLRuby/hdr_samples/dff_bench.rb +66 -0
  29. data/lib/HDLRuby/hdr_samples/dff_counter.rb +20 -0
  30. data/lib/HDLRuby/hdr_samples/include.rb +14 -0
  31. data/lib/HDLRuby/hdr_samples/instance_open.rb +23 -0
  32. data/lib/HDLRuby/hdr_samples/mei8.rb +256 -0
  33. data/lib/HDLRuby/hdr_samples/mei8_bench.rb +309 -0
  34. data/lib/HDLRuby/hdr_samples/multer_gen.rb +8 -0
  35. data/lib/HDLRuby/hdr_samples/multer_seq.rb +29 -0
  36. data/lib/HDLRuby/hdr_samples/neural/a.rb +9 -0
  37. data/lib/HDLRuby/hdr_samples/neural/a_sub.rb +5 -0
  38. data/lib/HDLRuby/hdr_samples/neural/bw.rb +23 -0
  39. data/lib/HDLRuby/hdr_samples/neural/counter.rb +16 -0
  40. data/lib/HDLRuby/hdr_samples/neural/dadz.rb +9 -0
  41. data/lib/HDLRuby/hdr_samples/neural/dadz_sub.rb +4 -0
  42. data/lib/HDLRuby/hdr_samples/neural/forward.rb +153 -0
  43. data/lib/HDLRuby/hdr_samples/neural/forward_sub.rb +62 -0
  44. data/lib/HDLRuby/hdr_samples/neural/forward_sub_rand.rb +41 -0
  45. data/lib/HDLRuby/hdr_samples/neural/forward_sub_rand_typedef.rb +47 -0
  46. data/lib/HDLRuby/hdr_samples/neural/mem.rb +30 -0
  47. data/lib/HDLRuby/hdr_samples/neural/random.rb +23 -0
  48. data/lib/HDLRuby/hdr_samples/neural/selector.rb +29 -0
  49. data/lib/HDLRuby/hdr_samples/neural/sigmoid.rb +20 -0
  50. data/lib/HDLRuby/hdr_samples/neural/z.rb +33 -0
  51. data/lib/HDLRuby/hdr_samples/prog.obj +256 -0
  52. data/lib/HDLRuby/hdr_samples/ram.rb +18 -0
  53. data/lib/HDLRuby/hdr_samples/register_with_code_bench.rb +98 -0
  54. data/lib/HDLRuby/hdr_samples/rom.rb +10 -0
  55. data/lib/HDLRuby/hdr_samples/struct.rb +14 -0
  56. data/lib/HDLRuby/hdr_samples/sumprod.rb +29 -0
  57. data/lib/HDLRuby/hdr_samples/sw_encrypt_bench.rb +103 -0
  58. data/lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb +261 -0
  59. data/lib/HDLRuby/hdr_samples/sw_encrypt_cpusim_bench.rb +302 -0
  60. data/lib/HDLRuby/hdr_samples/system_open.rb +11 -0
  61. data/lib/HDLRuby/hdr_samples/tuple.rb +16 -0
  62. data/lib/HDLRuby/hdr_samples/with_channel.rb +118 -0
  63. data/lib/HDLRuby/hdr_samples/with_class.rb +199 -0
  64. data/lib/HDLRuby/hdr_samples/with_decoder.rb +17 -0
  65. data/lib/HDLRuby/hdr_samples/with_fsm.rb +34 -0
  66. data/lib/HDLRuby/hdr_samples/with_reconf.rb +103 -0
  67. data/lib/HDLRuby/hdrcc.rb +623 -0
  68. data/lib/HDLRuby/high_samples/_adder_fault.rb +23 -0
  69. data/lib/HDLRuby/high_samples/_generic_transmission2.rb +146 -0
  70. data/lib/HDLRuby/high_samples/adder.rb +21 -0
  71. data/lib/HDLRuby/high_samples/adder_common_errors.rb +25 -0
  72. data/lib/HDLRuby/high_samples/addsub.rb +33 -0
  73. data/lib/HDLRuby/high_samples/addsubz.rb +37 -0
  74. data/lib/HDLRuby/high_samples/after.rb +28 -0
  75. data/lib/HDLRuby/high_samples/all_signals.rb +29 -0
  76. data/lib/HDLRuby/high_samples/alu.rb +61 -0
  77. data/lib/HDLRuby/high_samples/anonymous.rb +41 -0
  78. data/lib/HDLRuby/high_samples/before.rb +28 -0
  79. data/lib/HDLRuby/high_samples/blockblock.rb +26 -0
  80. data/lib/HDLRuby/high_samples/bugs/dadz.rb +22 -0
  81. data/lib/HDLRuby/high_samples/bugs/misample_instan.rb +20 -0
  82. data/lib/HDLRuby/high_samples/bugs/misample_updown.rb +22 -0
  83. data/lib/HDLRuby/high_samples/bugs/sample_add.rb +16 -0
  84. data/lib/HDLRuby/high_samples/bugs/sample_barrel.rb +13 -0
  85. data/lib/HDLRuby/high_samples/bugs/sample_daice.rb +57 -0
  86. data/lib/HDLRuby/high_samples/bugs/sample_kumiawase.rb +52 -0
  87. data/lib/HDLRuby/high_samples/bugs/sample_multi.rb +18 -0
  88. data/lib/HDLRuby/high_samples/bugs/sample_sub.rb +14 -0
  89. data/lib/HDLRuby/high_samples/bugs/z2.rb +32 -0
  90. data/lib/HDLRuby/high_samples/case.rb +32 -0
  91. data/lib/HDLRuby/high_samples/case2.rb +30 -0
  92. data/lib/HDLRuby/high_samples/change.rb +23 -0
  93. data/lib/HDLRuby/high_samples/clocks.rb +35 -0
  94. data/lib/HDLRuby/high_samples/comparer.rb +21 -0
  95. data/lib/HDLRuby/high_samples/conditionals.rb +29 -0
  96. data/lib/HDLRuby/high_samples/dff.rb +23 -0
  97. data/lib/HDLRuby/high_samples/each.rb +28 -0
  98. data/lib/HDLRuby/high_samples/exporter.rb +42 -0
  99. data/lib/HDLRuby/high_samples/functions.rb +60 -0
  100. data/lib/HDLRuby/high_samples/if_seq.rb +26 -0
  101. data/lib/HDLRuby/high_samples/inherit_as_dff.rb +32 -0
  102. data/lib/HDLRuby/high_samples/inherit_dff.rb +36 -0
  103. data/lib/HDLRuby/high_samples/instance.rb +37 -0
  104. data/lib/HDLRuby/high_samples/memory.rb +64 -0
  105. data/lib/HDLRuby/high_samples/multi_file.rb +27 -0
  106. data/lib/HDLRuby/high_samples/overload.rb +32 -0
  107. data/lib/HDLRuby/high_samples/paper_after.rb +49 -0
  108. data/lib/HDLRuby/high_samples/ram.rb +27 -0
  109. data/lib/HDLRuby/high_samples/registers.rb +139 -0
  110. data/lib/HDLRuby/high_samples/rom.rb +23 -0
  111. data/lib/HDLRuby/high_samples/scopeblockname.rb +37 -0
  112. data/lib/HDLRuby/high_samples/scopescope.rb +26 -0
  113. data/lib/HDLRuby/high_samples/shift.rb +31 -0
  114. data/lib/HDLRuby/high_samples/shift2.rb +40 -0
  115. data/lib/HDLRuby/high_samples/simple_instance.rb +31 -0
  116. data/lib/HDLRuby/high_samples/test_all.sh +10 -0
  117. data/lib/HDLRuby/high_samples/typedef.rb +24 -0
  118. data/lib/HDLRuby/high_samples/values.rb +70 -0
  119. data/lib/HDLRuby/high_samples/vector.rb +22 -0
  120. data/lib/HDLRuby/high_samples/with_decoder.rb +30 -0
  121. data/lib/HDLRuby/high_samples/with_fsm.rb +46 -0
  122. data/lib/HDLRuby/high_samples/with_pipe.rb +43 -0
  123. data/lib/HDLRuby/high_samples/with_seq.rb +25 -0
  124. data/lib/HDLRuby/hruby_bstr.rb +1085 -0
  125. data/lib/HDLRuby/hruby_check.rb +317 -0
  126. data/lib/HDLRuby/hruby_db.rb +432 -0
  127. data/lib/HDLRuby/hruby_error.rb +44 -0
  128. data/lib/HDLRuby/hruby_high.rb +4103 -0
  129. data/lib/HDLRuby/hruby_low.rb +4735 -0
  130. data/lib/HDLRuby/hruby_low2c.rb +1986 -0
  131. data/lib/HDLRuby/hruby_low2high.rb +738 -0
  132. data/lib/HDLRuby/hruby_low2seq.rb +248 -0
  133. data/lib/HDLRuby/hruby_low2sym.rb +126 -0
  134. data/lib/HDLRuby/hruby_low2vhd.rb +1437 -0
  135. data/lib/HDLRuby/hruby_low_bool2select.rb +295 -0
  136. data/lib/HDLRuby/hruby_low_cleanup.rb +193 -0
  137. data/lib/HDLRuby/hruby_low_fix_types.rb +437 -0
  138. data/lib/HDLRuby/hruby_low_mutable.rb +1803 -0
  139. data/lib/HDLRuby/hruby_low_resolve.rb +165 -0
  140. data/lib/HDLRuby/hruby_low_skeleton.rb +129 -0
  141. data/lib/HDLRuby/hruby_low_with_bool.rb +141 -0
  142. data/lib/HDLRuby/hruby_low_with_port.rb +167 -0
  143. data/lib/HDLRuby/hruby_low_with_var.rb +302 -0
  144. data/lib/HDLRuby/hruby_low_without_bit2vector.rb +88 -0
  145. data/lib/HDLRuby/hruby_low_without_concat.rb +162 -0
  146. data/lib/HDLRuby/hruby_low_without_connection.rb +113 -0
  147. data/lib/HDLRuby/hruby_low_without_namespace.rb +718 -0
  148. data/lib/HDLRuby/hruby_low_without_outread.rb +107 -0
  149. data/lib/HDLRuby/hruby_low_without_select.rb +206 -0
  150. data/lib/HDLRuby/hruby_serializer.rb +398 -0
  151. data/lib/HDLRuby/hruby_tools.rb +37 -0
  152. data/lib/HDLRuby/hruby_types.rb +239 -0
  153. data/lib/HDLRuby/hruby_values.rb +64 -0
  154. data/lib/HDLRuby/hruby_verilog.rb +1888 -0
  155. data/lib/HDLRuby/hruby_verilog_name.rb +52 -0
  156. data/lib/HDLRuby/low_samples/adder.yaml +97 -0
  157. data/lib/HDLRuby/low_samples/after.yaml +228 -0
  158. data/lib/HDLRuby/low_samples/before.yaml +223 -0
  159. data/lib/HDLRuby/low_samples/blockblock.yaml +48 -0
  160. data/lib/HDLRuby/low_samples/bugs/sample_add.yaml +97 -0
  161. data/lib/HDLRuby/low_samples/bugs/sample_daice.yaml +444 -0
  162. data/lib/HDLRuby/low_samples/bugs/sample_kumiawase.yaml +332 -0
  163. data/lib/HDLRuby/low_samples/bugs/sample_sub.yaml +97 -0
  164. data/lib/HDLRuby/low_samples/bugs/seqpar.yaml +184 -0
  165. data/lib/HDLRuby/low_samples/case.yaml +327 -0
  166. data/lib/HDLRuby/low_samples/change.yaml +135 -0
  167. data/lib/HDLRuby/low_samples/clocks.yaml +674 -0
  168. data/lib/HDLRuby/low_samples/cloner.rb +22 -0
  169. data/lib/HDLRuby/low_samples/comparer.yaml +85 -0
  170. data/lib/HDLRuby/low_samples/conditionals.yaml +133 -0
  171. data/lib/HDLRuby/low_samples/dff.yaml +107 -0
  172. data/lib/HDLRuby/low_samples/each.yaml +1328 -0
  173. data/lib/HDLRuby/low_samples/exporter.yaml +226 -0
  174. data/lib/HDLRuby/low_samples/functions.yaml +298 -0
  175. data/lib/HDLRuby/low_samples/generic_transmission.yaml +597 -0
  176. data/lib/HDLRuby/low_samples/inherit_as_dff.yaml +125 -0
  177. data/lib/HDLRuby/low_samples/inherit_dff.yaml +107 -0
  178. data/lib/HDLRuby/low_samples/load_yaml.rb +11 -0
  179. data/lib/HDLRuby/low_samples/memory.yaml +678 -0
  180. data/lib/HDLRuby/low_samples/namespace_extractor.rb +23 -0
  181. data/lib/HDLRuby/low_samples/overload.yaml +226 -0
  182. data/lib/HDLRuby/low_samples/paper_after.yaml +431 -0
  183. data/lib/HDLRuby/low_samples/port_maker.rb +14 -0
  184. data/lib/HDLRuby/low_samples/ram.yaml +207 -0
  185. data/lib/HDLRuby/low_samples/registers.yaml +228 -0
  186. data/lib/HDLRuby/low_samples/rom.yaml +2950 -0
  187. data/lib/HDLRuby/low_samples/shift.yaml +230 -0
  188. data/lib/HDLRuby/low_samples/shift2.yaml +2095 -0
  189. data/lib/HDLRuby/low_samples/simple_instance.yaml +102 -0
  190. data/lib/HDLRuby/low_samples/test_all.sh +43 -0
  191. data/lib/HDLRuby/low_samples/typedef.yaml +115 -0
  192. data/lib/HDLRuby/low_samples/values.yaml +577 -0
  193. data/lib/HDLRuby/low_samples/variable_maker.rb +14 -0
  194. data/lib/HDLRuby/low_samples/vector.yaml +56 -0
  195. data/lib/HDLRuby/low_samples/with_seq.yaml +188 -0
  196. data/lib/HDLRuby/low_samples/yaml2hdr.rb +10 -0
  197. data/lib/HDLRuby/low_samples/yaml2vhd.rb +19 -0
  198. data/lib/HDLRuby/sim/Makefile +19 -0
  199. data/lib/HDLRuby/sim/hruby_sim.h +590 -0
  200. data/lib/HDLRuby/sim/hruby_sim_calc.c +2362 -0
  201. data/lib/HDLRuby/sim/hruby_sim_core.c +589 -0
  202. data/lib/HDLRuby/sim/hruby_sim_list.c +93 -0
  203. data/lib/HDLRuby/sim/hruby_sim_vizualize.c +91 -0
  204. data/lib/HDLRuby/sim/hruby_value_pool.c +64 -0
  205. data/lib/HDLRuby/std/channel.rb +354 -0
  206. data/lib/HDLRuby/std/clocks.rb +165 -0
  207. data/lib/HDLRuby/std/counters.rb +82 -0
  208. data/lib/HDLRuby/std/decoder.rb +214 -0
  209. data/lib/HDLRuby/std/fsm.rb +516 -0
  210. data/lib/HDLRuby/std/pipeline.rb +220 -0
  211. data/lib/HDLRuby/std/reconf.rb +309 -0
  212. data/lib/HDLRuby/test_hruby_bstr.rb +2259 -0
  213. data/lib/HDLRuby/test_hruby_high.rb +594 -0
  214. data/lib/HDLRuby/test_hruby_high_low.rb +99 -0
  215. data/lib/HDLRuby/test_hruby_low.rb +934 -0
  216. data/lib/HDLRuby/v_samples/adder.v +10 -0
  217. data/lib/HDLRuby/v_samples/dff.v +12 -0
  218. data/lib/HDLRuby/v_samples/ram.v +20 -0
  219. data/lib/HDLRuby/v_samples/rom.v +270 -0
  220. data/lib/HDLRuby/version.rb +3 -0
  221. data/lib/HDLRuby.rb +11 -0
  222. data/makedoc +1 -0
  223. data/metadata.yaml +4 -0
  224. metadata +299 -0
@@ -0,0 +1,23 @@
1
+ ##
2
+ # Module generating a neuron bias or weight.
3
+ # Params:
4
+ # - +typ+: the data type of the neural signals.
5
+ # - +init_bw+: initial value of the bias/weight.
6
+ system :bw do |typ,init_bw|
7
+ # The control signals.
8
+ input :clk, :reset # Clock and reset
9
+ input :select_initial # Initialization of the bias/weight
10
+ input :select_update # Update of the bias/weight
11
+
12
+ # Update of the bias/weight
13
+ typ.input :dbw
14
+ # Output of the bias/weight
15
+ typ.output :bwo
16
+
17
+ # Behavior controlling the bias/weight
18
+ par(clk.posedge) do
19
+ hif(reset == 1) { bwo <= 0 }
20
+ helsif(select_initial == 1) { bwo <= init_bw }
21
+ helsif(select_update == 1) { bwo <= bwo+dbw }
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ ##
2
+ # Module generating the sample counter.
3
+ # Params:
4
+ # - +num+: the number of samples.
5
+ system :counter do |typ,num|
6
+ # The input control signals.
7
+ input :clk, :reset
8
+ # The output counter.
9
+ typ.output :out
10
+
11
+ par(clk.posedge) do
12
+ hif(reset == 1) { out <= 0 }
13
+ helsif(out == num-1) { out <= 0 }
14
+ helse { out <= out+1 }
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ system :dadz do |width,derivate|
2
+ input :clk,:res
3
+ signed[width].input :a
4
+ signed[width].output :dadz
5
+
6
+ par(clk.posedge) do
7
+ dadz <= mux(res == 1, 0, derivate.(a))
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require "./dadz.rb"
2
+
3
+ system :dadz_sub, dadz(32, proc {|a| (_sh01000000-a)*a }) do
4
+ end
@@ -0,0 +1,153 @@
1
+ require "./counter.rb"
2
+ require "./selector.rb"
3
+ require "./bw.rb"
4
+ require "./z.rb"
5
+ require "./a.rb"
6
+ require "./mem.rb"
7
+
8
+
9
+ ##
10
+ # Module Generating a foward propagation structure of a NN.
11
+ # Params:
12
+ # - +typ+: the data type for standard computations.
13
+ # - +arch+: the architecture of the NN as a array of columns sizes
14
+ # - +samps+: the NN input and expected outputs samples as a 3D array
15
+ # - +b_init+:the bias initial values as a 2D array
16
+ # - +w_init+:the weights initial values as a 3D array
17
+ # - +actP+: the activation proc, default: ReLU
18
+ # - +sopP+: the sum of production function, default: basic operators
19
+ system :forward do |typ,arch,samps,b_init,w_init,
20
+ actP = proc { |z| mux(z < 0, 0, z) },
21
+ sopP = proc do |xs,ws|
22
+ xs.zip(ws).map{ |x,w| x*w }.reduce(:+)
23
+ end |
24
+ ###
25
+ # The interface signals
26
+
27
+ # The control signals
28
+ input :clk, :reset
29
+ input :din, :select_initial
30
+ # The input signals for updating of the weights and biases,
31
+ # and the output signals giving the corresponding current values.
32
+ cap_bs = []
33
+ bs = []
34
+ cap_ws = []
35
+ ws = []
36
+ arch[1..-1].each.with_index do |col,i|
37
+ cap_bs << []
38
+ bs << []
39
+ # The biase update inputs and value outputs
40
+ col.times do |j|
41
+ cap_bs[-1] << ( typ.input :"cap_b#{i+1}_#{j+1}" )
42
+ bs[-1] << ( typ.output :"b#{i+1}_#{j+1}" )
43
+ end
44
+ # The weigh update inputs and value outputs
45
+ cap_ws << []
46
+ ws << []
47
+ col.times do |j0|
48
+ cap_ws[-1] << []
49
+ ws[-1] << []
50
+ arch[i].times do |j1|
51
+ cap_ws[-1][-1] << ( typ.input :"cap_w#{i+1}_#{j0+1}#{j1+1}" )
52
+ ws[-1][-1] << ( typ.output :"w#{i+1}_#{j0+1}#{j1+1}" )
53
+ end
54
+ end
55
+ end
56
+ # The output signals giving each neuron output.
57
+ as = []
58
+ arch[1..-1].each.with_index do |col,i|
59
+ as << []
60
+ col.times do |j|
61
+ as[-1] << ( typ.output :"a#{i+1}_#{j+1}" )
62
+ end
63
+ end
64
+ # The output signals indicating the current NN input and expected
65
+ # answer (they are provided by an inner memory).
66
+ ks = []
67
+ arch[0].times do |i|
68
+ # NN input
69
+ ks << ( typ.output :"k#{i+1}" )
70
+ end
71
+ ts = []
72
+ arch[-1].times do |i|
73
+ # NN expected answer
74
+ ts << ( typ.output :"t#{i+1}" )
75
+ end
76
+
77
+ ###
78
+ # The inner signals
79
+
80
+ # The control signals
81
+ inner :select_update
82
+ typedef(:outT) { [Math::log2(samps[0][0].size).ceil] } # Sample counter type
83
+ outT.inner :out
84
+
85
+ # The neurons sum results.
86
+ zs = []
87
+ arch[1..-1].each.with_index do |col,i|
88
+ zs << []
89
+ col.times do |j|
90
+ zs[-1] << ( typ.inner :"z#{i+1}_#{j+1}" )
91
+ end
92
+ end
93
+
94
+ ###
95
+ # The structural description (instantiation of thre neuron computation
96
+ # systems)
97
+
98
+ # Sample counter
99
+ counter(outT,samps[0][0].size).(:counterI).(clk, reset, out)
100
+
101
+ # Neuron update selector, the skip size is (NN depth)*4+1
102
+ # (4-cycle neurons and one addition sync cycle).
103
+ selector(arch.size*4+1).(:selectorI).(clk, reset, select_update)
104
+
105
+ # Input and expected output memories
106
+ arch[0].times do |i|
107
+ # Samples for input i
108
+ mem(typ,outT,samps[0][i]).(:"k#{i+1}I").(clk, din, out, ks[i])
109
+ end
110
+ arch[-1].times do |i|
111
+ # Expected values for output i
112
+ mem(typ,outT,samps[1][i]).(:"t#{i+1}I").(clk, din, out, ts[i])
113
+ end
114
+
115
+ # Biases and weights
116
+ arch[1..-1].each.with_index do |col,i|
117
+ # Biases
118
+ col.times do |j|
119
+ bw(typ,b_init[i][j]).
120
+ (:"b#{i+1}_#{j+1}I").(clk, reset, cap_bs[i][j],
121
+ select_initial, select_update, bs[i][j])
122
+ end
123
+ # Weights
124
+ col.times do |j0|
125
+ arch[i].times do |j1|
126
+ bw(typ,w_init[i][j0][j1]).
127
+ (:"w#{i+1}_#{j0+1}#{j1+1}I").(clk,reset,cap_ws[i][j0][j1],
128
+ select_initial, select_update, ws[i][j0][j1])
129
+ end
130
+ end
131
+ end
132
+
133
+ # Weighted Sums
134
+ # First column
135
+ arch[1].times do |j|
136
+ z(typ,ks.size,sopP).(:"z2_#{j+1}I").
137
+ (clk, reset, *ks, *ws[0][j], bs[0][j], zs[0][j])
138
+ end
139
+ # Other columns
140
+ arch[2..-1].each.with_index do |col,i|
141
+ col.times do |j|
142
+ z(typ,as[i].size,sopP).(:"z#{i+3}_#{j+1}I").
143
+ (clk, reset, *as[i], *ws[i+1][j], bs[i+1][j], zs[i+1][j])
144
+ end
145
+ end
146
+
147
+ # Activations
148
+ arch[1..-1].each.with_index do |col,i|
149
+ col.times do |j|
150
+ a(typ,actP).(:"a#{i+1}_#{j+1}I").(clk, reset, zs[i][j], as[i][j])
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,62 @@
1
+ require "./forward.rb"
2
+ require "./sigmoid.rb"
3
+
4
+ ##
5
+ # A fully specified forward module with 8.24-bit fixed point computation
6
+ # and 4.4bit table-based sigmoid activation function.
7
+ # Structure: 2 inputs, one 3-column hidden layer and 2 outputs.
8
+
9
+ system :forward_sub, forward(
10
+ signed[31..0], # Data type
11
+ [2,3,2], # NN structure
12
+ [
13
+ # Input samples.
14
+ # First input.
15
+ [[_sh08000000, _sh08000000, _sh05000000, _sh05000000,
16
+ *([_sh00000000]*28)],
17
+ # Second input.
18
+ [_sh08000000, _sh05000000, _sh08000000, _sh05000000,
19
+ *([_sh00000000]*28)]],
20
+ # Expected outputs
21
+ # First output
22
+ [[_sh01000000, _sh00000000, _sh00000000, _sh00000000,
23
+ *([_sh00000000]*28)],
24
+ # Second output
25
+ [_sh00000000, _sh01000000, _sh01000000, _sh01000000,
26
+ *([_sh00000000]*28)]]
27
+ ],
28
+ # Biases initial values
29
+ [
30
+ # Second column
31
+ [_shFF000000, _shFF000000, _shFF000000],
32
+ # Third column
33
+ [_shFF000000, _shFF000000]
34
+ ],
35
+ # Weights initial values
36
+ [
37
+ # Second column
38
+ [
39
+ # First neuron
40
+ [ _sh00199999, _sh00666666 ],
41
+ # Second neuron
42
+ [ _sh004CCCCC, _sh00800000 ],
43
+ # Third neuron
44
+ [ _sh00999999, _sh00199999 ]
45
+ ],
46
+ # Third column
47
+ [
48
+ # First neuron
49
+ [ _sh00B33333, _sh00333333, _sh0014CCCC ],
50
+ # Second neuron
51
+ [ _sh00333333, _sh00800000, _sh01199999 ]
52
+ ]
53
+ ],
54
+ # The activation function.
55
+ proc{|addr| sigmoid(8,4,32,24,addr)},
56
+ # The sum of production function.
57
+ proc do |xs,ws|
58
+ (xs.zip(ws).map { |x,w| x*w }.reduce(:+))[27..20]
59
+ end
60
+ ) do
61
+ end
62
+
@@ -0,0 +1,41 @@
1
+ require "./forward.rb"
2
+ require "./sigmoid.rb"
3
+ require "./random.rb"
4
+
5
+ ##
6
+ # A fully specified forward module with 8.24-bit fixed point computation
7
+ # and 4.4bit table-based sigmoid activation function.
8
+ # Structure: 2 inputs, one 3-column hidden layer and 2 outputs.
9
+
10
+ system :forward_sub, forward(
11
+ signed[31..0], # Data type
12
+ [2,4,3,2], # NN structure
13
+ [
14
+ # Input samples.
15
+ # First input.
16
+ [[_sh08000000, _sh08000000, _sh05000000, _sh05000000,
17
+ *([_sh00000000]*28)],
18
+ # Second input.
19
+ [_sh08000000, _sh05000000, _sh08000000, _sh05000000,
20
+ *([_sh00000000]*28)]],
21
+ # Expected outputs
22
+ # First output
23
+ [[_sh01000000, _sh00000000, _sh00000000, _sh00000000,
24
+ *([_sh00000000]*28)],
25
+ # Second output
26
+ [_sh00000000, _sh01000000, _sh01000000, _sh01000000,
27
+ *([_sh00000000]*28)]]
28
+ ],
29
+ # Biases initial values
30
+ rand_array([4,3,2],32),
31
+ # Weights initial values
32
+ rand_array([[2,2,2,2],[4,4,4],[3,3]],32),
33
+ # The activation function.
34
+ proc{|addr| sigmoid(8,4,32,24,addr)},
35
+ # The sum of production function.
36
+ proc do |xs,ws|
37
+ (xs.zip(ws).map { |x,w| x*w }.reduce(:+))[27..20]
38
+ end
39
+ ) do
40
+ end
41
+
@@ -0,0 +1,47 @@
1
+ require "./forward.rb"
2
+ require "./sigmoid.rb"
3
+ require "./random.rb"
4
+
5
+ # proc do |xs,ws|
6
+ # (xs.zip(ws).map { |x,w| x*w }.reduce(:+))[27..20]
7
+ # end
8
+
9
+ # 16:16 Fixed-point type.
10
+ [31..0].signed.typedef(:fix32)
11
+ # Multiply must adjust point.
12
+ fix32.define_operator(:*) do |left,right|
13
+ ( left.as([31..0]) * right ) >> 32
14
+ end
15
+
16
+
17
+ ##
18
+ # A fully specified forward module with 8.24-bit fixed point computation
19
+ # and 4.4bit table-based sigmoid activation function.
20
+ # Structure: 2 inputs, one 3-column hidden layer and 2 outputs.
21
+
22
+ system :forward_sub, forward(
23
+ fix32, # Data type
24
+ [2,4,3,2], # NN structure
25
+ [
26
+ # Input samples.
27
+ # First input.
28
+ [[_sh08000000, _sh08000000, _sh05000000, _sh05000000,
29
+ *([_sh00000000]*28)],
30
+ # Second input.
31
+ [_sh08000000, _sh05000000, _sh08000000, _sh05000000,
32
+ *([_sh00000000]*28)]],
33
+ # Expected outputs
34
+ # First output
35
+ [[_sh01000000, _sh00000000, _sh00000000, _sh00000000,
36
+ *([_sh00000000]*28)],
37
+ # Second output
38
+ [_sh00000000, _sh01000000, _sh01000000, _sh01000000,
39
+ *([_sh00000000]*28)]]
40
+ ],
41
+ # Biases initial values
42
+ rand_array([4,3,2],32),
43
+ # Weights initial values
44
+ rand_array([[2,2,2,2],[4,4,4],[3,3]],32)
45
+ ) do
46
+ end
47
+
@@ -0,0 +1,30 @@
1
+ ##
2
+ # Module generating a lookup table memory.
3
+ # Params:
4
+ # - +inT+: the data type of the input (address).
5
+ # - +outT+: the data type of the output (data).
6
+ # - +ar+: the contents of the memory as an array.
7
+ system :mem do |inT,outT,ar|
8
+ # The control signals.
9
+ input :clk # Clock
10
+ input :din # Read enable
11
+
12
+ # The interface signals.
13
+ inT.input :addr # Address
14
+ outT.output :data # Data
15
+
16
+ # The memory.
17
+ outT[ar.size].inner :contents
18
+
19
+ # Fills the memory (static)
20
+ # ar.each.with_index do |val,i|
21
+ # contents[i] <= val
22
+ # end
23
+ contents <= ar
24
+
25
+ # Handle the access to the memory.
26
+ par(clk.posedge) do
27
+ hif(din == 1) { data <= contents[addr] }
28
+ helse { data <= :"_#{"z"*outT.width}" }
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ # Generate a +width+ bit (signed) random value.
2
+ def rand_signed(width)
3
+ :"_s#{width.times.map { Random.rand(0..1).to_s }.join }".to_expr
4
+ end
5
+
6
+ # Generates a N-dimension array described by +geometry+ filled
7
+ # with +width+ bit values.
8
+ def rand_array(geometry,width)
9
+ if geometry.is_a?(Array) then
10
+ # Geometry is hierarchical, recurse on it.
11
+ return geometry.map {|elem| rand_array(elem,width) }
12
+ else
13
+ # Geometry is a size of a 1-D array, generate it.
14
+ return geometry.times.map { |i| rand_signed(width) }
15
+ end
16
+ end
17
+
18
+ # rand_array(4,6).each do |v|
19
+ # puts v.content
20
+ # end
21
+ #
22
+ # system :test do
23
+ # end
@@ -0,0 +1,29 @@
1
+ ##
2
+ # Module generating the selector that decides if a neuron is to update or not.
3
+ # Params:
4
+ # - +skip+: the number of clocks to skip.
5
+ system :selector do |skip|
6
+ input :clk, :reset
7
+ output :enable_update
8
+
9
+ # The clock counter.
10
+ [Math::log2(skip).ceil].inner :counter
11
+
12
+ # The behavior handling the skip counter
13
+ par(clk.posedge) do
14
+ hif(reset == 1) { counter <= 0 }
15
+ helse do
16
+ hif(counter == skip-1) { counter <=0 }
17
+ helse {counter <= counter + 1 }
18
+ end
19
+ end
20
+
21
+ # The behavior handling the update signal
22
+ par(clk.posedge) do
23
+ hif(reset == 1) { enable_update <= 0 }
24
+ helse do
25
+ hif(counter == skip-1) { enable_update <= 1 }
26
+ helse { enable_update <= 0 }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ def sigmoid(a_width, a_point, d_width, d_point, addr)
2
+ # Initialize the result to force it as a ruby variable.
3
+ High.cur_system.open do
4
+ sub do
5
+ # Generates the rom
6
+ [d_width][2**a_width].inner :contents
7
+ contents <= (2**a_width).times.map do |i|
8
+ # Converts i to a float
9
+ i = i.to_f * 2**(-a_point)
10
+ # Compute the sigmoid
11
+ sigm = (1.0 / (1+Math.exp(i)))
12
+ # Convert it to fixed point
13
+ (sigm * 2**d_point).to_i
14
+ end
15
+
16
+ # Use it for the access
17
+ contents[addr[(a_point+d_width-1-d_point)..a_point-d_point]]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ ##
2
+ # Module generating a neuron sum.
3
+ # Params:
4
+ # - +typ+: the data type of the neural signals.
5
+ # - +ary+: the arity of the neuron.
6
+ # - +sopP+: the sum of product function.
7
+ system :z do |typ,ary,sopP|
8
+ # The control signals.
9
+ input :clk, :reset # Clock and reset
10
+
11
+ # The inputs of the neuron.
12
+ ins = []
13
+ wgs = []
14
+ ary.times do |i|
15
+ # The input values
16
+ ins << typ.input(:"x#{i}")
17
+ end
18
+ ary.times do |i|
19
+ # The weights
20
+ wgs << typ.input(:"w#{i}")
21
+ end
22
+ # The bias
23
+ typ.input :bias
24
+
25
+ # The sum output
26
+ typ.output :z
27
+
28
+ # Behavior controlling the bias/weight
29
+ par(clk.posedge) do
30
+ hif(reset == 1) { z <= 0 }
31
+ helse { z <= sopP.(ins + [1],wgs + [bias]) }
32
+ end
33
+ end