OpenCores
URL https://opencores.org/ocsvn/soc_maker/soc_maker/trunk

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [hdl_coder.rb] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 feddischso
###############################################################
2
#
3
#  File:      hdl_coder.rb
4
#
5
#  Author:    Christian Hättich
6
#
7
#  Project:   System-On-Chip Maker
8
#
9
#  Target:    Linux / Windows / Mac
10
#
11
#  Language:  ruby
12
#
13
#
14
###############################################################
15
#
16
#
17
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
18
#
19
#   This program is free software: you can redistribute it and/or modify
20
#   it under the terms of the GNU General Public License as published by
21
#   the Free Software Foundation, either version 3 of the License, or
22
#   (at your option) any later version.
23
#
24
#   This program is distributed in the hope that it will be useful,
25
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
#   GNU General Public License for more details.
28
#
29
#   You should have received a copy of the GNU General Public License
30
#   along with this program.  If not, see .
31
#
32
#
33
###############################################################
34
#
35
#   Description:
36
#     This file contains two HDL-coders:
37
#      - VHDLCoder
38
#      - VerilogCoder (not implemented, yet)
39
#
40
#
41
#
42
###############################################################
43
 
44
module SOCMaker
45
class HDLCoder
46
 
47
  def initialize
48
    @decl_part  = "";   # declaration
49
    @asgn_part  = "";   # assignment
50
    @inst_part  = "";   # instantiation
51
 
52
 
53
 
54
  end
55
 
56
 
57
end
58
 
59
 
60
class VerilogCoder < HDLCoder
61
  #TODO
62 7 feddischso
  #
63
  #
64
 
65
  def filename( name )
66
    return name + ".v"
67
  end
68
 
69 3 feddischso
end
70
 
71
 
72
class VHDLCoder < HDLCoder
73
 
74
 
75
 
76
 
77
  #
78
  # Add a component declaration to the declaration-string @decl_part
79
  # This for example looks like
80
  # component <> is
81
  #  generic(
82
  #    g1 : ...
83
  #    g2 : ...
84
  #    ...
85
  #    );
86
  #  port(
87
  #    p1 : ...
88
  #    p2 : ...
89
  #    p3 : ...
90
  #    ...
91
  #    )
92
  #  end component <>;
93
  #
94
  # In addition, we add some VHDL comments (author, mail, license)
95
  #
96 7 feddischso
  def add_core_component( core_name, core_spec )
97 5 feddischso
 
98 3 feddischso
    @decl_part << "--\n"
99
    @decl_part << "-- core author: #{core_spec.author} - #{core_spec.authormail}\n"
100
    @decl_part << "-- license: #{core_spec.license}\n"
101
    @decl_part << "--\n"
102
    @decl_part << "component #{core_spec.toplevel} is\n"
103
    generic_str = entity_generic_str( core_spec );
104
    @decl_part << "generic ( #{ generic_str  });\n" if generic_str.size > 0
105
    @decl_part << "port( \n" << entity_port_str( core_spec ) <<" );\n"
106
    @decl_part << "end component #{core_spec.toplevel};\n"
107 7 feddischso
    #entity_generic_str( core_spec )
108 3 feddischso
  end
109
 
110 7 feddischso
  def filename( name )
111
    return name + ".vhd"
112
  end
113 3 feddischso
 
114
 
115
  def entity_generic_str( core )
116
 
117
    generic_str = ""
118
    core.generics do |gen_name, gen_type, gen_val, is_last|
119
      generic_str     << gen_name << " : " << gen_type << " := " << gen_val.to_s
120
      generic_str     << ";" unless is_last
121
      generic_str     << "\n"
122
    end
123
    return generic_str
124
  end
125
 
126
 
127
  #
128
  # Create a string, which lists all signals of 'core'
129
  # We iterate over all interface:
130
  # For each interface, we iterate over all ports:
131
  # For each port, we lookup the definition in the 'soc_lib' and
132
  # add the VHDL code according to the definition as string to
133
  # port_string
134
  #
135
  def entity_port_str( core )
136
    port_string = ""
137
 
138
    core.ports do |port_name, port_dir, port_len, is_last |
139
 
140
      # The string we are add in every iteration looks for example like
141
      #    myportname1 :  out std_logic_vector( 6-1 downto 0 )
142
      #    or
143
      #    myportname2 :  in  std_logic
144
      #
145
      port_string << port_name.to_s << " : "
146
 
147 7 feddischso
      puts port_name.to_s + ": dir: " + port_dir.to_s + ", len: " + port_len.to_s
148
 
149
 
150 3 feddischso
      # port direction
151
      if    port_dir == 2
152
        port_string << " inout "
153
      elsif port_dir == 1
154
        port_string << " in "
155
      else
156
        port_string << " out "
157
      end
158
 
159
      # port type / length
160
      if(   port_len.is_a?( String ) ||
161
          ( port_len.is_a?( Fixnum ) && port_len > 1 )
162
        )
163
        port_string << " std_logic_vector( #{port_len}-1 downto 0 ) "
164
      elsif ( port_len.is_a?( Fixnum ) && port_len == 1 )
165
        port_string << " std_logic "
166
      else
167 7 feddischso
        puts "FAILED " + port_len.to_s #TODO
168 3 feddischso
      end
169
 
170
      # end of the line
171
      port_string << ";" unless is_last
172
      port_string << "\n"
173
    end
174
    return port_string
175
  end
176
 
177
 
178 7 feddischso
  def add_core_instance( inst_name, inst )
179 3 feddischso
 
180
    @inst_part << inst_name << " : " << inst.defn.toplevel << "\n"
181
    generic_str = ""
182
    inst.generics do |generic, type, value, is_last|
183
      generic_str << "#{generic} => #{value}"
184
      generic_str << "," unless is_last
185
      generic_str << "\n"
186
    end
187
    @inst_part << "generic map( \n#{generic_str} )\n" if generic_str.size > 0
188
    port_str = ""
189 7 feddischso
    inst.ports do |port_name, dir, length, is_last|
190 3 feddischso
      port_str << "#{port_name} => #{inst_name}_#{port_name}"
191
      port_str << "," unless is_last
192
      port_str << "\n"
193
      if length > 1
194
        @decl_part << "signal #{inst_name}_#{port_name} : std_logic_vector( #{length}-1 downto 0 );\n"
195
      else
196
        @decl_part << "signal #{inst_name}_#{port_name} : std_logic;\n"
197
      end
198
    end
199
    @inst_part << "port map( \n#{port_str} );\n\n\n" if port_str.size > 0
200
 
201
  end
202
 
203
 
204
 
205 7 feddischso
  def add_ifc_connection( ifc_spec, ifc_name, length, src_inst, dst_inst, src_ifc, dst_ifc )
206 3 feddischso
 
207 7 feddischso
    ###
208
    #
209
    # declaration
210
    #
211
    #
212 3 feddischso
    ifc_spec.ports.each do |port_name, port|
213
      @decl_part << "signal #{ifc_name}_#{port_name.to_s} : "
214
      if length[ port_name ] > 1
215
        @decl_part << " std_logic_vector( #{length[ port_name ]}-1 downto 0 ) "
216
      else
217
        @decl_part << " std_logic "
218
      end
219
      # end of the line
220
      @decl_part << ";\n"
221
    end
222
 
223
 
224 7 feddischso
    ###
225
    #
226
    # assignment
227
    #
228
    #
229 3 feddischso
    ifc_spec.ports.each do |port_name, port_dir|
230
      if port_dir == 0
231
        src_inst_sel = src_inst
232
        dst_inst_sel = dst_inst
233
        src_ifc_sel  = src_ifc
234
        dst_ifc_sel  = dst_ifc
235
      else
236
        src_inst_sel = dst_inst
237
        dst_inst_sel = src_inst
238
        src_ifc_sel  = dst_ifc
239
        dst_ifc_sel  = src_ifc
240
      end
241
 
242
 
243 5 feddischso
      # length == 0 means, that no
244
      # signal is assigned to this connection
245
      if length[ port_name ] > 0
246 3 feddischso
 
247 5 feddischso
        port_tmp_name = "#{ifc_name}_#{port_name.to_s}"
248 3 feddischso
 
249 5 feddischso
 
250
        # combine all sources
251
        tmp = "#{port_tmp_name} <= "
252 7 feddischso
        assigned = false
253
 
254 5 feddischso
        # loop over instances
255
        src_inst_sel.each_with_index do |(inst_name, inst), i|
256
          ( tmp_name, port) = inst.get_port( src_ifc_sel[ inst_name ], port_name )
257
          if port != nil
258
            tmp << "\"" + "0" * ( length[ port_name ] - port[ :len ] ) + "\" & "  if port[ :len ] < length[ port_name ]
259
            tmp << "#{inst_name}_#{tmp_name}"
260
            tmp << " and \n" unless i == src_inst_sel.size-1
261 7 feddischso
            assigned = true
262 5 feddischso
          end
263
        end
264 3 feddischso
        tmp << ";\n"
265 7 feddischso
        @asgn_part << tmp if assigned
266 5 feddischso
 
267 7 feddischso
 
268
        puts src_inst_sel.size
269
        puts tmp
270
 
271
 
272
        tmp = ""
273
        assigned = false
274 5 feddischso
        # assign to destination
275
        dst_inst_sel.each_with_index do |(inst_name, inst), i|
276 7 feddischso
          if not inst == nil  #TODO
277
            ( tmp_name, port) = inst.get_port( dst_ifc_sel[ inst_name ], port_name )
278
            if port != nil
279
              tmp << "#{inst_name}_#{tmp_name} <= #{port_tmp_name}"
280
              tmp << "( #{port[ :len ]}-1 downto 0 )" if port[ :len ] > 1
281
              tmp << ";\n"
282
              assigned = true
283
            end
284 5 feddischso
          end
285
        end
286 7 feddischso
        @asgn_part << tmp if assigned
287 3 feddischso
      end
288
 
289
    end
290
  end
291
 
292
 
293
 
294 7 feddischso
  def get_hdl_code( soc, entity_name )
295 5 feddischso
    add_toplevel_sig( soc, entity_name )
296
    entity_str = SOCMaker::conf[ :LIC ].split(/\n/).map{ |s| "-- "+s }.join("\n") + "\n"
297 3 feddischso
    entity_str << "-- Auto-Generated by #{SOCMaker::conf[ :app_name ]} \n"
298
    entity_str << "-- Date: #{Time.now}\n"
299
    entity_str << SOCMaker::conf[ :vhdl_include ] + "\n"
300
    entity_str << "entity #{entity_name} is \n"
301
    tmp = entity_port_str( soc )
302
    entity_str << "port( \n" << tmp << " );\n" if tmp.size > 0
303
    entity_str << "end entity #{entity_name};\n\n\n"
304
    entity_str << "ARCHITECTURE IMPL of #{entity_name} is \n"
305
    entity_str << @decl_part
306
    entity_str << "\n\n"
307
    entity_str << "begin"
308
    entity_str << "\n\n"
309
    entity_str << "--"
310
    entity_str << "-- assignments "
311
    entity_str << "--"
312
    entity_str << "\n\n"
313
    entity_str << @asgn_part
314
    entity_str << "\n\n"
315
    entity_str << "--"
316
    entity_str << "-- instances "
317
    entity_str << "--"
318
    entity_str << "\n\n"
319
    entity_str << @inst_part
320
    entity_str << "end ARCHITECTURE IMPL;"
321
    return entity_str
322
  end
323
 
324 5 feddischso
  def add_toplevel_sig( soc, entity_name )
325 7 feddischso
    soc.ports do |port_name, dir, length, is_last|
326
      if dir == 0
327
        @asgn_part << "#{port_name} <= #{entity_name}_#{port_name}"
328
      else
329
        @asgn_part << "#{entity_name}_#{port_name} <= #{port_name} "
330
      end
331
      @asgn_part << ";\n"
332 5 feddischso
      if length > 1
333
        @decl_part << "signal #{entity_name}_#{port_name} : std_logic_vector( #{length}-1 downto 0 );\n"
334
      else
335
        @decl_part << "signal #{entity_name}_#{port_name} : std_logic;\n"
336
      end
337
    end
338
  end
339 3 feddischso
 
340
 
341
 
342
  def asgn_str( ifc_spec, con,  ifc_name, core1, core1_name, core2, core2_name )
343
    port_string = ""
344
 
345
    { core1 => [ con[ :ifc1 ], :src1 ],
346
      core2 => [ con[ :ifc2 ], :src2 ] }.each do| core, tmp |
347
 
348
      core.ports( tmp[0] ) do | port_spec_name, port_name, port_dir |
349
 
350
        if port_dir  == 0
351
          port_string << "#{ifc_name}_#{port_spec_name}"
352
          port_string << " <= "
353
          port_string << "#{con[ tmp[1] ]}_#{port_name}"
354
          port_string << ";\n"
355
        else
356
          port_string << "#{con[ tmp[1] ]}_#{port_name}"
357
          port_string << " <= "
358
          port_string << "#{ifc_name}_#{port_spec_name }"
359
          port_string << ";\n"
360
        end
361
      end
362
 
363
    end
364
 
365
    return port_string
366
  end
367
 
368
 
369
 
370
 
371
 
372
end
373
end
374
 
375
# vim: noai:ts=2:sw=2

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.