Class: HDLRuby::Viz::IC

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/hruby_viz.rb

Overview

An IC block

Defined Under Namespace

Classes: Route, Tile

Constant Summary collapse

EPSILON =
0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, parent = nil, system = nil) ⇒ IC

Returns a new instance of IC.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/HDLRuby/hruby_viz.rb', line 54

def initialize(name, type, parent = nil, system = nil)
  @name = name.to_s
  @type = type.to_sym
  @parent = parent
  @parent.children << self if @parent
  @system = system
  @children = []
  @branches = []
  @ports = []
  @lports = []; @uports = []; @rports = []; @dports = []
  @xpos   = 0
  @ypos   = 0
  @width  = 0      # Width in number of routes
  @height = 0      # Height in number of routes
  @matrix   = [[]] # The matrix separting the children
  @retries = 100   # Number of resize of matrix and retry of route.
  @rheigths = []   # The heights of each in number of routes
  @cwidths  = []   # The widths of each column in number of routes
  # @border   = 2    # The IC border size for routing to external ports.
  @border   =  4   # The IC border size for routing to external ports.
  @cell_border = 4 # The cell border size
  @routes   = []   # The connection routes.
  @port_width = 3  # The width in tiles for a port
  @scale = 1.0     # The scale for SVG generation
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



45
46
47
# File 'lib/HDLRuby/hruby_viz.rb', line 45

def box
  @box
end

#branchesObject (readonly)

Returns the value of attribute branches.



42
43
44
# File 'lib/HDLRuby/hruby_viz.rb', line 42

def branches
  @branches
end

#childrenObject (readonly)

Returns the value of attribute children.



42
43
44
# File 'lib/HDLRuby/hruby_viz.rb', line 42

def children
  @children
end

#cwidthsObject (readonly)

Returns the value of attribute cwidths.



46
47
48
# File 'lib/HDLRuby/hruby_viz.rb', line 46

def cwidths
  @cwidths
end

#dportsObject (readonly)

Returns the value of attribute dports.



43
44
45
# File 'lib/HDLRuby/hruby_viz.rb', line 43

def dports
  @dports
end

#heightObject

Returns the value of attribute height.



44
45
46
# File 'lib/HDLRuby/hruby_viz.rb', line 44

def height
  @height
end

#idCObject (readonly)

Returns the value of attribute idC.



2622
2623
2624
# File 'lib/HDLRuby/hruby_viz.rb', line 2622

def idC
  @idC
end

#lportsObject (readonly)

Returns the value of attribute lports.



43
44
45
# File 'lib/HDLRuby/hruby_viz.rb', line 43

def lports
  @lports
end

#matrixObject (readonly)

Returns the value of attribute matrix.



46
47
48
# File 'lib/HDLRuby/hruby_viz.rb', line 46

def matrix
  @matrix
end

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/HDLRuby/hruby_viz.rb', line 42

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



42
43
44
# File 'lib/HDLRuby/hruby_viz.rb', line 42

def parent
  @parent
end

#port_widthObject (readonly)

Returns the value of attribute port_width.



49
50
51
# File 'lib/HDLRuby/hruby_viz.rb', line 49

def port_width
  @port_width
end

#portsObject (readonly)

Returns the value of attribute ports.



43
44
45
# File 'lib/HDLRuby/hruby_viz.rb', line 43

def ports
  @ports
end

#rheightsObject (readonly)

Returns the value of attribute rheights.



46
47
48
# File 'lib/HDLRuby/hruby_viz.rb', line 46

def rheights
  @rheights
end

#route_heightObject (readonly)

Returns the value of attribute route_height.



47
48
49
# File 'lib/HDLRuby/hruby_viz.rb', line 47

def route_height
  @route_height
end

#route_matrixObject (readonly)

Returns the value of attribute route_matrix.



47
48
49
# File 'lib/HDLRuby/hruby_viz.rb', line 47

def route_matrix
  @route_matrix
end

#route_widthObject (readonly)

Returns the value of attribute route_width.



47
48
49
# File 'lib/HDLRuby/hruby_viz.rb', line 47

def route_width
  @route_width
end

#routesObject (readonly)

Returns the value of attribute routes.



48
49
50
# File 'lib/HDLRuby/hruby_viz.rb', line 48

def routes
  @routes
end

#rportsObject (readonly)

Returns the value of attribute rports.



43
44
45
# File 'lib/HDLRuby/hruby_viz.rb', line 43

def rports
  @rports
end

#scaleObject

Returns the value of attribute scale.



50
51
52
# File 'lib/HDLRuby/hruby_viz.rb', line 50

def scale
  @scale
end

#systemObject (readonly)

The instanciated system if it is an instance.



52
53
54
# File 'lib/HDLRuby/hruby_viz.rb', line 52

def system
  @system
end

#typeObject (readonly)

Returns the value of attribute type.



42
43
44
# File 'lib/HDLRuby/hruby_viz.rb', line 42

def type
  @type
end

#uportsObject (readonly)

Returns the value of attribute uports.



43
44
45
# File 'lib/HDLRuby/hruby_viz.rb', line 43

def uports
  @uports
end

#widthObject

Returns the value of attribute width.



44
45
46
# File 'lib/HDLRuby/hruby_viz.rb', line 44

def width
  @width
end

#xposObject

Returns the value of attribute xpos.



44
45
46
# File 'lib/HDLRuby/hruby_viz.rb', line 44

def xpos
  @xpos
end

#yposObject

Returns the value of attribute ypos.



44
45
46
# File 'lib/HDLRuby/hruby_viz.rb', line 44

def ypos
  @ypos
end

Instance Method Details

#[](idx) ⇒ Object

Get a port by index.



99
100
101
# File 'lib/HDLRuby/hruby_viz.rb', line 99

def [](idx)
  return @ports[idx]
end

#add_port(name, direction, type = :signal) ⇒ Object

Add a new port.



86
87
88
89
90
# File 'lib/HDLRuby/hruby_viz.rb', line 86

def add_port(name,direction, type=:signal)
  port = Port.new(name,self,direction,type)
  @ports << port
  return port
end

#adjacentsObject

Get the adjacent IC



145
146
147
# File 'lib/HDLRuby/hruby_viz.rb', line 145

def adjacents
  return @ports.map {|p| p.targets.map {|t| t.ic } }.flatten
end

#alu_svg(ic) ⇒ Object

Generate an ALU description SVG text for +ic+



2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
# File 'lib/HDLRuby/hruby_viz.rb', line 2376

def alu_svg(ic)
  id = Viz.to_svg_id(ic.name)
  # Determine the side of the inputs (and consequently of the outputs),
  # and the number of inputs.
  iside = LEFT # Default side: left
  inum = 0
  ic.ports.each do |port|
    if port.direction == :input then
      iside = port.side
      inum += 1
    end
  end
  # NOTE: inum is zero in case of a constant, force at least 1.
  inum = 1 if inum < 1
  # The length of a leg
  if iside == LEFT or iside == RIGHT then
    leg = ic.height / inum
  else
    leg = ic.width / inum
  end
  # Generate the resulting polygon.
  res = "<polygon id=\"#{id}\" fill=\"#eee\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "points=\""
  # The result depends on the size of the input.
  case(iside)
  when LEFT
    # The left side
    res += "#{ic.xpos*@scale} #{(ic.ypos)*@scale} "
    (inum-1).times do |i|
      res += "#{(ic.xpos)*@scale} #{(ic.ypos+i*leg+0.25)*@scale} "
      res += "#{(ic.xpos)*@scale} #{(ic.ypos+(i+1)*leg-0.25)*@scale} "
      res += "#{(ic.xpos+0.25)*@scale} #{(ic.ypos+(i+1)*leg)*@scale} "
      res += "#{(ic.xpos)*@scale} #{(ic.ypos+(i+1)*leg+0.25)*@scale} "
    end
    res += "#{(ic.xpos)*@scale} #{(ic.ypos+inum*leg)*@scale} "
    # The up side
    # res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+inum*leg-1)*@scale} "
    res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+ic.height*0.7)*@scale} "
    # The right side.
    # res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+1)*@scale}"
    res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+ic.height*0.3)*@scale}"
    # The down side is not necessary, close the shape.
    res += "\"/>"
  when UP
    # The right side
    res += "#{(ic.xpos)*@scale} #{(ic.ypos+ic.height)*@scale} "
    (inum-1).times do |i|
      res += "#{(ic.xpos+i*leg+0.25)*@scale} #{(ic.ypos+ic.height)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg-0.25)*@scale} #{(ic.ypos+ic.height)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg)*@scale} #{(ic.ypos-0.25+ic.height)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg+0.25)*@scale} #{(ic.ypos+ic.height)*@scale} "
    end
    res += "#{(ic.xpos+inum*leg)*@scale} #{(ic.ypos+ic.height)*@scale} "
    # The up side
    # res += "#{(ic.xpos+inum*leg-1)*@scale} #{(ic.ypos)*@scale} "
    res += "#{(ic.xpos+ic.width*0.7)*@scale} #{(ic.ypos)*@scale} "
    # The right side.
    # res += "#{(ic.xpos+1)*@scale} #{(ic.ypos)*@scale}"
    res += "#{(ic.xpos+ic.width*0.3)*@scale} #{(ic.ypos)*@scale}"
    # The down side is not necessary, close the shape.
    res += "\"/>"
  when RIGHT
    # The right side
    res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos)*@scale} "
    (inum-1).times do |i|
      res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+i*leg+0.25)*@scale} "
      res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+(i+1)*leg-0.25)*@scale} "
      res += "#{(ic.xpos-0.25+ic.width)*@scale} #{(ic.ypos+(i+1)*leg)*@scale} "
      res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+(i+1)*leg+0.25)*@scale} "
    end
    res += "#{(ic.xpos+ic.width)*@scale} #{(ic.ypos+inum*leg)*@scale} "
    # The up side
    # res += "#{(ic.xpos)*@scale} #{(ic.ypos+inum*leg-1)*@scale} "
    res += "#{(ic.xpos)*@scale} #{(ic.ypos+ic.height*0.7)*@scale} "
    # The right side.
    # res += "#{(ic.xpos)*@scale} #{(ic.ypos+1)*@scale}"
    res += "#{(ic.xpos)*@scale} #{(ic.ypos+ic.height*0.3)*@scale}"
    # The down side is not necessary, close the shape.
    res += "\"/>"
  when DOWN
    # The down side
    res += "#{ic.xpos*@scale} #{(ic.ypos)*@scale} "
    (inum-1).times do |i|
      res += "#{(ic.xpos+i*leg+0.25)*@scale} #{(ic.ypos)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg-0.25)*@scale} #{(ic.ypos)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg)*@scale} #{(ic.ypos+0.25)*@scale} "
      res += "#{(ic.xpos+(i+1)*leg+0.25)*@scale} #{(ic.ypos)*@scale} "
    end
    res += "#{(ic.xpos+inum*leg)*@scale} #{(ic.ypos)*@scale} "
    # The up side
    # res += "#{(ic.xpos+inum*leg-1)*@scale} #{(ic.ypos+ic.height)*@scale} "
    res += "#{(ic.xpos+ic.width*0.7)*@scale} #{(ic.ypos+ic.height)*@scale} "
    # The right side.
    # res += "#{(ic.xpos+1)*@scale} #{(ic.ypos+ic.height)*@scale}"
    res += "#{(ic.xpos+ic.width*0.3)*@scale} #{(ic.ypos+ic.height)*@scale}"
    # The down side is not necessary, close the shape.
    res += "\"/>"
  else
    raise "Wrong side: #{iside}"
  end
  # Its name.
  # sy = (iside == UP || iside == DOWN) ? 0 : -0.5 # Shift to avoid ports
  sy = (iside == UP || iside == DOWN || inum.even?) ? 0 : -0.5 # Shift to avoid ports
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0 + sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#bounding_childrenObject

Compute the bounding box of the current placement.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/HDLRuby/hruby_viz.rb', line 475

def bounding_children
  x0 = 1/0.0
  x1 = -1/0.0
  y0 = 1/0.0
  y1 = -1/0.0
  @children.each do |child|
    x0 = child.xpos if child.xpos < x0
    x1 = child.xpos+child.width if child.xpos+child.width > x1
    y0 = child.ypos if child.ypos < y0
    y1 = child.ypos+child.height if child.ypos+child.height > y1
  end
  @box = [x0,y0,x1,y1]
  return @box
end

#center_childrenObject

Centers the global position of the current placement.



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/HDLRuby/hruby_viz.rb', line 491

def center_children
  x0,y0,x1,y1 = @box
  if x0 < x1 then
    dx = -x0 - (x1-x0) / 2.0
  else
    dx = -x1 - (x0-x1) / 2.0
  end
  if y0 < y1 then
    dy = -y0 - (y1-y0) / 2.0
  else
    dy = -y1 - (y0-y1) / 2.0
  end
  @children.each do |child| 
    child.xpos += dx
    child.ypos += dy
  end
end

#clocked_process_svg(ic) ⇒ Object

Generate a clocked process description SVG text for +ic+



2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
# File 'lib/HDLRuby/hruby_viz.rb', line 2261

def clocked_process_svg(ic)
  id = Viz.to_svg_id(ic.name)
  res = "<rect fill=\"#ddd\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/32.0}\" " +
    "x=\"#{(ic.xpos-1/16.0)*@scale}\" y=\"#{(ic.ypos-1/16.0)*@scale}\" " +
    "rx=\"#{(1+1/16.0)*@scale}\" " +
    "width=\"#{(ic.width+1/8.0)*@scale}\" "+
    "height=\"#{(ic.height+1/8.0)*@scale}\"/>\n"
  res += "<rect id=\"#{id}\" fill=\"#ddd\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/32.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "rx=\"#{@scale}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  # Its name.
  sy = (ic.lports.size.even? and ic.rports.size.even? ) ? 0 : -0.5 # Shift to avoid ports
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0 + sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#compress_route_tilesObject

Compress the route matrix and update the position of the objects accordignly.



1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
# File 'lib/HDLRuby/hruby_viz.rb', line 1902

def compress_route_tiles
  # First record the position of each port of current IC for
  # easily updating their position.
  lports_row = []
  self.lports.each {|p| lports_row[p.ypos] = p }
  uports_col = []
  self.uports.each {|p| uports_col[p.xpos] = p }
  rports_row = []
  self.rports.each {|p| rports_row[p.ypos] = p }
  dports_col = []
  self.dports.each {|p| dports_col[p.xpos] = p }

  # Remove the route matrix lines whose tiles are empty or contain
  # vertical wires only.
  dh = 0 # Total height reduction.
  @route_matrix.size.times do |y|
    row = @route_matrix[y-dh]
    # puts "y=#{y} dh=#{dh} y-dh=#{y-dh}, @route_matrix.size=#{@route_matrix.size}"
    # Check if the row is a canditate for deletion.
    del = row.none? do |tile|
      tile && (tile.ic || tile.wires.any? {|d| d!=UP|DOWN } )
    end
    # But maybe some IC will touch is each other or the border if the
    # row is deleted,
    # we want to avoid it.
    if del then
      if y-dh == 0 then
        del = false if @route_matrix[y-dh+1].any? { |tile| tile.ic }
      elsif y-dh == @route_matrix.size - 1 then 
        del = false if @route_matrix[y-dh-1].any? { |tile| tile.ic }
      else
        del = false if row.size.times.any? do |x|
          @route_matrix[y-dh-1][x].ic and @route_matrix[y-dh+1][x].ic
        end
      end
    end
    # Delete the row if possible.
    if del then
      @route_matrix.delete_at(y-dh)
      # Update the current IC ports array rows.
      lports_row.delete_at(y-dh)
      rports_row.delete_at(y-dh)
      dh += 1
    end
  end
  # Remove the route matrix column whose tiles are empty or contain
  # horizontal wires only.
  dw = 0
  self.width.times do |x|
    del = @route_matrix.none? do |row|
      tile = row[x-dw]
      tile && (tile.ic || tile.wires.any? {|d| d!=LEFT|RIGHT } )
    end
    # But maybe some IC will touch is each other or the border if the
    # row is deleted,
    # we want to avoid it.
    if del then
      if x-dw == 0 then
        del = false if @route_matrix.any? { |row| row[x-dw+1].ic }
      elsif x-dw == @route_matrix[0].size - 1 then 
        del = false if @route_matrix.any? { |row| row[x-dw-1].ic }
      else
        del = false if @route_matrix.size.times.any? do |y|
          @route_matrix[y][x-dw-1].ic and @route_matrix[y][x-dw+1].ic
        end
      end
    end
    if del then
      # Remove the column.
      @route_matrix.each {|row| row.delete_at(x-dw) }
      # Update the current IC ports array columns.
      uports_col.delete_at(x-dw)
      dports_col.delete_at(x-dw)
      dw += 1
    end
  end

  # # Readd border if an ic is touching a border.
  # if @route_matrix.any? {|row| row[0] && row[0].ic } then
  #   # An IC touch the left border, add an empty colum.
  #   dw -= 1
  #   @route_matrix.each { |row| row.unshift(Tile.new) }
  # end
  # if @route_matrix.any? {|row| row[-1] && row[-1].ic } then
  #   # An IC touch the right border, add an empty column.
  #   dw -= 1
  #   @route_matrix.each { |row| row.push(Tile.new) }
  # end
  
  puts "Compressed tiles result: width -#{dw}, height -#{dh}"
  
  # Update the position of the IC and their ports according to the
  # new matrix. For that purpose, scan the matrix for left to right
  # and down to up update the position of the encountered tile's ic
  # when encountered first.
  fixed = Set.new
  @route_matrix.each_with_index do |row,y|
    row.each_with_index do |tile,x|
      # Check if there is an IC to process.
      next unless (tile and tile.ic and !fixed.include?(tile.ic))
      # Yes, update its own and its ports' positions.
      dx = x - tile.ic.xpos
      dy = y - tile.ic.ypos
      tile.ic.xpos += dx
      tile.ic.ypos += dy
      tile.ic.ports.each do |port|
        port.xpos += dx
        port.ypos += dy
      end
      # Tell it is already fixed.
      fixed.add(tile.ic)
    end
  end

  # Update the size of the current IC and the position of its ports.
  self.width -= dw
  self.height -= dh
  # Position with border (since the IC has been resized).
  self.rports.each { |port| port.xpos -= dw }
  self.uports.each { |port| port.ypos -= dh }
  # Position in border.
  lports_row.each_with_index { |port,y| port.ypos = y if port }
  uports_col.each_with_index { |port,x| port.xpos = x if port }
  rports_row.each_with_index { |port,y| port.ypos = y if port }
  dports_col.each_with_index { |port,x| port.xpos = x if port }
end

#connect(p0, p1) ⇒ Object

Connect two ports +p0+ and +p1+



104
105
106
107
108
# File 'lib/HDLRuby/hruby_viz.rb', line 104

def connect(p0,p1)
  puts "connect #{p0.name} of #{p0.ic.name} to #{p1.name} of #{p1.ic.name}"
  p0.targets << p1
  p1.targets << p0
end

#connection_route(port0, port1) ⇒ Object

Route from +port0+ to +port1. NOTE: uses the A* algorithm with taxi cab distance.



1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
# File 'lib/HDLRuby/hruby_viz.rb', line 1648

def connection_route(port0,port1)
  puts "From port: #{port0.name} (ic: #{port0.ic.name}) " + "to port: #{port1.name} (ic: #{port1.ic.name})"

  pos0 = [port0.xpos,port0.ypos]
  pos1 = [port1.xpos,port1.ypos]
  oset = [pos0]
  # oset = Set.new
  oset << pos0
  from = { }
  # gscore = Hash.new(1/0.0)
  # gscore[pos0] = 0
  gscore = Array.new(@route_matrix.size) { Array.new(@route_matrix[0].size) { 1/0.0 } }
  gscore[pos0[1]][pos0[0]] = 0
  # fscore = Hash.new(1/0.0)
  # fscore[pos0] = taxi_distance(pos0,pos1)
  fscore = Array.new(@route_matrix.size) { [] }
  fscore[pos0[1]][pos0[0]] = taxi_distance(pos0,pos1)
  while oset.any? do
    # Pick the position from oset with the minimum fscore.
    cpos = nil          # Current position
    mscore = 1/0.0      # Minimum score
    # oset.each do |pos|
    #   # score = fscore[pos]
    #   score = fscore[pos[1]][pos[0]]
    #   if score < mscore then
    #     mscore = score
    #     cpos = pos
    #   end
    # end
    # The best score is necessily at the end of oset.
    cpos = oset.pop
    # puts "cpos=#{cpos}"
    if touch?(cpos,pos1) then
      # The goal is reached.
      from[pos1] = cpos
      return reconstruct_path(port0,port1,from,pos1)
    end
    # oset.delete(cpos) # No need anymore since pop
    # Get the neighbor positions for port.
    # poses = free_neighbors(port0,cpos)
    poses = free_neighbors(port0,port1,cpos)
    poses.each do |pos|
      # Try it.
      # tscore = gscore[cpos] + cost_position(port0,pos)
      tscore = gscore[cpos[1]][cpos[0]] + cost_position(port0,pos)
      # if tscore < gscore[pos] then
      if tscore < gscore[pos[1]][pos[0]] then
        # This path to neigbor is better than any previous one, keep it.
        from[pos]   = cpos
        # gscore[pos] = tscore
        gscore[pos[1]][pos[0]] = tscore
        # fscore[pos] = tscore + taxi_distance(pos,pos1)
        fscore[pos[1]][pos[0]] = tscore + taxi_distance(pos,pos1)
        # oset << pos unless oset.include?(pos)
        idx = oset.bsearch_index {|p| fscore[p[1]][p[0]] <= fscore[pos[1]][pos[0]] }
        if idx then
          oset.insert(idx,pos)
        else
          oset << pos
        end
      end
    end
  end
  return false
end

#cost_position(port, pos) ⇒ Object

Compute the cost of a position +pos+ relatively to +port+.



1574
1575
1576
1577
# File 'lib/HDLRuby/hruby_viz.rb', line 1574

def cost_position(port,pos)
  elem = @route_matrix[pos[1]][pos[0]]
  return (elem == nil or elem == port) ? 0 : 1
end

#distance(ic0, ic1) ⇒ Object

Compute the distance between to IC.



155
156
157
158
159
# File 'lib/HDLRuby/hruby_viz.rb', line 155

def distance(ic0,ic1)
  res = Math.sqrt((ic0.xpos-ic1.xpos)**2+(ic0.ypos-ic1.ypos)**2)
  # res = (ic0.xpos-ic1.xpos).abs+(ic0.ypos-ic1.ypos).abs
  return res
end

#down_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate a down port description SVG text.



2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
# File 'lib/HDLRuby/hruby_viz.rb', line 2567

def down_port_svg(name,type,xpos,ypos,width,height)
  case type
  when :posedge
    res = "<rect fill=\"#88f\" stroke=\"#000\" " 
  when :negedge
    res = "<rect fill=\"#f88\" stroke=\"#000\" " 
  else
    res = "<rect fill=\"#ff0\" stroke=\"#000\" " 
  end
  res += "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos+width/2.0},#{ypos} #{xpos},#{ypos+height} " +
    "#{xpos+width},#{ypos+height}\"/>\n"
  return res
end

#forceX(ic, t = 0.0) ⇒ Object

Compute the X force applied on a (assumed) child +ic+.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/HDLRuby/hruby_viz.rb', line 179

def forceX(ic, t=0.0)
  k = Math.sqrt(1.0/@children.size)
  # Repulive force.
  repulsive = @children.reduce(0.0) do |sum,child|
    # Skip ic as repulsive contribution.
    next sum if child == ic
    d = distance(ic,child)
    # Ensure d is not 0 to avoid infinity.
    d = EPSILON if d < EPSILON
    sum + ((k**2) / d) * xrate(ic,child)
  end
  # Limit repulsive to avoid explosion.
  puts "repulsive X=#{repulsive}"
  # Attractive force.
  attractive = (ic.adjacents).reduce(0.0) do |sum,adj|
    # Skip world as adjacent.
    next sum if adj == self
    # puts "ic=#{ic.name} adj=#{adj.name}"
    # Compute the attractive contribution of adj.
    d = distance(ic,adj)
    force = -(xrate(ic,adj)*d**2)/k
    sum + force
  end
  puts "attractive X=#{attractive}"
  # Returns the resulting force.
  return (repulsive + attractive)*Math.exp(-t)
end

#forceY(ic, t = 0.0) ⇒ Object

Compute the Y force applied on a (assumed) child +ic+.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/HDLRuby/hruby_viz.rb', line 208

def forceY(ic, t=0.0)
  k = Math.sqrt(1.0/@children.size)
  # Repulive force.
  repulsive = @children.reduce(0.0) do |sum,child|
    # Skip ic as repulsive contribution
    next sum if child == ic
    d = distance(ic,child)
    # Ensure d is not 0 to avoid infinity.
    d = EPSILON if d < EPSILON
    sum + ((k**2) / d) * yrate(ic,child)
  end
  puts "repulsive Y=#{repulsive}"
  # Attractive force.
  attractive = (ic.adjacents).reduce(0.0) do |sum,adj|
    # Skip world as adjacent.
    next sum if adj == self
    # Compute the attractive contribution of adj.
    d = distance(ic,adj)
    force = -(yrate(ic,adj)*d**2)/k
    sum + force
  end
  puts "attractive Y=#{attractive}"
  # Returns the resulting force.
  # return (repulsive + attractive).round(@children.size)
  # return repulsive + attractive
  return (repulsive + attractive)*Math.exp(-t)
end

#free_neighbors(port0, port1, cpos) ⇒ Object

Get the neighbor free positions for port. def free_neighbors(port,cpos)



1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
# File 'lib/HDLRuby/hruby_viz.rb', line 1535

def free_neighbors(port0,port1,cpos)
  res = []
  # Left neighbor.
  lpos = [cpos[0]-1,cpos[1]]
  if lpos[0] >= 0 then
    elem = @route_matrix[lpos[1]][lpos[0]]
    # res << lpos if elem.free_from_right?(port)
    res << lpos if elem.free_from_right?(port0,port1) and
      !ic_port_conflict(port0,port1,[lpos[0]-1,lpos[1]],RIGHT)
  end
  # Up neighbor.
  upos = [cpos[0],cpos[1]+1]
  if upos[1] < @route_height then
    elem = @route_matrix[upos[1]][upos[0]]
    # res << upos if elem.free_from_down?(port)
    res << upos if elem.free_from_down?(port0,port1) and
      !ic_port_conflict(port0,port1,[upos[0],upos[1]+1],DOWN)
  end
  # Right neighbor.
  rpos = [cpos[0]+1,cpos[1]]
  if rpos[0] < @route_width then
    elem = @route_matrix[rpos[1]][rpos[0]]
    # res << rpos if elem.free_from_left?(port)
    res << rpos if elem.free_from_left?(port0,port1) and
      !ic_port_conflict(port0,port1,[rpos[0]+1,rpos[1]],LEFT)
  end
  # Down neighbor.
  dpos = [cpos[0],cpos[1]-1]
  if dpos[1] >= 0 then
    elem = @route_matrix[dpos[1]][dpos[0]]
    # res << dpos if elem.free_from_up?(port)
    res << dpos if elem.free_from_up?(port0,port1) and
      !ic_port_conflict(port0,port1,[dpos[0],dpos[1]-1],UP)
  end
  # Return the free neigbor positions.
  return res
end

#height_childrenObject

Compute the global height of the current placement.



510
511
512
513
514
515
516
517
518
# File 'lib/HDLRuby/hruby_viz.rb', line 510

def height_children
  y0 = 1/0.0
  y1 = -1/0.0
  @children.each do |child|
    y0 = child.ypos if child.ypos < y0
    y1 = child.ypos if child.ypos > y1
  end
  return y1-y0
end

#ic_port_conflict(port0, port1, pos, side) ⇒ Object

Check if there is a port at location +pos+ in +side+ that conflict with both +port0+ and +port1+.



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
# File 'lib/HDLRuby/hruby_viz.rb', line 1509

def ic_port_conflict(port0,port1,pos,side)
  return false unless @route_matrix[pos[1]]
  return false unless @route_matrix[pos[1]][pos[0]]
  ic = @route_matrix[pos[1]][pos[0]].ic
  return false unless ic # No IC here, so not possible port to conflict
  # There is an IC get the port at pos from side if any.
  p = nil
  case side
  when LEFT
    port = ic.lports.find {|p| p.xpos==pos[0] && p.ypos==pos[1] }
  when UP
    port = ic.uports.find {|p| p.xpos==pos[0] && p.ypos==pos[1] }
  when RIGHT
    port = ic.rports.find {|p| p.xpos==pos[0] && p.ypos==pos[1] }
  when DOWN
    port = ic.dports.find {|p| p.xpos==pos[0] && p.ypos==pos[1] }
  end
  if port and port != port0 and port != port1 then
    return true
  else
    return false
  end
end

#init_routeObject

Create the global routing matrix.



1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/HDLRuby/hruby_viz.rb', line 1477

def init_route
  # Build the matrix.
  @route_height = @rheights.reduce(:+)
  @route_width  = @cwidths.reduce(:+)
  @route_matrix = []
  @route_height.times do
    row = []
    @route_matrix << row
    @route_width.times { row << Tile.new }
  end
  # Fill it with the ic.
  puts "@route_width=#{@route_width} @route_height=#{@route_height}"
  @children.each do |child|
    x0 = child.xpos
    y0 = child.ypos
    # puts "child=#{child.name} x0=#{x0} y0=#{y0} child.height=#{child.height} child.width=#{child.width}"
    child.height.times do |y|
      child.width.times do |x|
        @route_matrix[y0+y][x0+x].ic = child
      end
    end
  end
end

#instance_svg(ic) ⇒ Object

Generate an instance description SVG text for +ic+



2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
# File 'lib/HDLRuby/hruby_viz.rb', line 2215

def instance_svg(ic)
  id = Viz.to_svg_id(ic.name)
  # The rectangle representing the instance.
  res = "<rect id=\"#{id}\" fill=\"#eee\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/12.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  # Its name.
  sy = (ic.lports.size.even? and ic.rports.size.even? ) ? 0 : -0.5 # Shift to avoid ports
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0 + sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#left_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate a left port description SVG text.



2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
# File 'lib/HDLRuby/hruby_viz.rb', line 2493

def left_port_svg(name,type,xpos,ypos,width,height)
  uX = width/16.0
  uY = height/16.0
  case type
  when :posedge
    res = "<rect fill=\"#88f\" stroke=\"#000\" " 
  when :negedge
    res = "<rect fill=\"#f88\" stroke=\"#000\" " 
  else
    res = "<rect fill=\"#ff0\" stroke=\"#000\" " 
  end
  res += "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos+height/2.0} #{xpos+width},#{ypos} " +
    "#{xpos+width},#{ypos+height}\"/>\n"
  return res
end

#left_right_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate a left-right port description SVG text.



2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
# File 'lib/HDLRuby/hruby_viz.rb', line 2533

def left_right_port_svg(name,type,xpos,ypos,width,height)
  res = "<rect fill=\"#FF0\" stroke=\"#000\" " +
    "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos+height/2} #{xpos+width},#{ypos} " +
    "#{xpos+width},#{ypos+height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos} #{xpos+width},#{ypos+height/2.0} " +
    "#{xpos},#{ypos+height}\"/>\n"
  return res
end

#matrix_childrenObject

Compute the matrix that isolate each children in current placement.



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/HDLRuby/hruby_viz.rb', line 552

def matrix_children
  if @children.size == 1 then
    # Only one child.
    @matrix = [ [@children[0]] ]
    return @matrix
  end
  x0,y0,x1,y1 = @box
  # puts "box=#{@box}"
  width = x1-x0
  height = y1-y0
  nrows = 1
  ncols = 1
  @matrix = nil
  dir = :x
  separate = false
  while !separate do
    @matrix = []
    nrows.times { @matrix << [] }
    xstep = width / nrows
    ystep = height / ncols
    # puts "ncols=#{ncols} nrows=#{nrows} xstep=#{xstep} ystep=#{ystep}"
    separate = true
    @children.each do |child|
      col = ((child.xpos - x0) / xstep).floor
      col = ncols-1 if col >= ncols
      row = ((child.ypos - y0) / ystep).floor
      row = nrows-1 if row >= nrows
      # puts "xpos=#{child.xpos} ypos=#{child.ypos} col=#{col} row=#{row}"
      if matrix[row][col] then
        separate = false
        break
      else
        matrix[row][col] = child
      end
    end
    if dir == :x then
      ncols += 1
      dir = :y
    else
      nrows += 1
      dir = :x
    end
  end
  # Ensures the matrix rows are of identical size.
  ncols = @matrix.map {|row| row.size}.max
  @matrix.each {|row| row[ncols-1] = nil unless row[ncols-1] }

  # Compress the matrix by mergin colums and rows when possible
  # (i.e. not more than one children per cell.
  # For the rows.
  compressed= []
  nrows = @matrix.size
  ncols = @matrix[0].size
  puts "Before compression: nrows=#{nrows} ncols=#{ncols}"
  cidx = 0
  while(cidx < nrows) do
    # Add a new compressed row.
    compressed << @matrix[cidx].clone
    # Try to merge following rows to it.
    nidx = cidx + 1
    while (nidx < nrows) do
      if ncols.times.any? {|i| compressed[-1][i] and @matrix[nidx][i] } then
        # Cannot merge, stop compression of current row here.
        # (subtract 1 since cidx is increased after loop).
        cidx = nidx-1
        break
      else
        # Can merge, compress, and try to merge again.
        @matrix[nidx].each.with_index do |child,i|
          compressed[-1][i] = child if child
        end
        nidx += 1
        cidx += 1
      end
    end
    cidx += 1
  end
  # Update the matrix with the compression result.
  @matrix = compressed
  # Now compress for the columns.
  nrows = @matrix.size
  compressed= []
  nrows.times { compressed << [] }
  cidx = 0
  while(cidx < ncols) do
    # Add a new compressed column.
    @matrix.each_with_index {|row,i| compressed[i] << row[cidx] }
    # Try to merge following columns to it.
    nidx = cidx + 1
    while (nidx < ncols) do
      if nrows.times.any? {|i| compressed[i][-1] and @matrix[i][nidx] } then
        # Cannot merge, stop compression of current column here.
        # (subtract 1 since cidx is increased after loop).
        cidx = nidx-1
        break
      else
        # Can merge, compress, and try to merge again.
        @matrix.each.with_index do |row,i|
          child = row[nidx]
          compressed[i][-1] = child if child
        end
        nidx += 1
        cidx += 1
      end
    end
    cidx += 1
  end
  # Update the matrix with the compression result.
  @matrix = compressed

  puts "After compression: nrows=#{nrows} ncols=#{ncols}"

  # Returns the resulting matrix.
  return @matrix
end

#memory_svg(ic) ⇒ Object

Generate a memory description SVG text for +ic+



2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
# File 'lib/HDLRuby/hruby_viz.rb', line 2336

def memory_svg(ic)
  id = Viz.to_svg_id(ic.name)
  res = "<rect id=\"#{id}\" fill=\"#fff\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "rx=\"#{@scale/4}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  res += "<rect fill=\"#fff\" stroke=\"#333\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x=\"#{(ic.xpos+1/4.0)*@scale}\" "+
    "y=\"#{(ic.ypos+1/4.0)*@scale}\" " +
    "width=\"#{(ic.width-1/2.0)*@scale}\" "+
    "height=\"#{(ic.height-1/2.0)*@scale}\"/>\n"
  res += "<line stroke=\"#333\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x1=\"#{(ic.xpos+1/4.0+1/8.0)*@scale}\" "+
    "y1=\"#{(ic.ypos+1/4.0)*@scale}\" " +
    "x2=\"#{(ic.xpos+1/4.0+1/8.0)*@scale}\" "+
    "y2=\"#{(ic.ypos+ic.height-1/4.0)*@scale}\"/>\n"
  res += "<line stroke=\"#333\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x1=\"#{(ic.xpos+1/4.0)*@scale}\" "+
    "y1=\"#{(ic.ypos+1/4.0+1/8.0)*@scale}\" " +
    "x2=\"#{(ic.xpos+ic.width-1/4.0)*@scale}\" "+
    "y2=\"#{(ic.ypos+1/4.0+1/8.0)*@scale}\"/>\n"
  # Its name.
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0 + 1/10.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-1.0)*@scale,
                           0.6*@scale)
  return res
end

#minimize_height_childrenObject

Rotate the current placement to minimize its global height.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/HDLRuby/hruby_viz.rb', line 533

def minimize_height_children
  dt = 0.01
  t = dt
  minh = 1/0.0
  mint = t
  while t < 2*Math::PI do
    h = self.height_children
    if minh > h then
      mint = t
      minh = h
    end
    rotate_children(dt)
    t += dt
  end
  rotate_children(mint+dt)
end

#normalize_size_childrenObject

Normalize the size of the children so than children with same number of statements have the same area, if they are of the same kind, they have the same shape, if the areas of the children are in the same order as their number of statements. However, registers and memory are not processed here (yet?).



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/HDLRuby/hruby_viz.rb', line 743

def normalize_size_children
  # Group the non-register and non-memory children by number of 
  # statements.
  by_stmnts = Hash.new {|h,k| h[k] = [] }
  @children.each do |child|
    if child.type != :register and child.type != :memory then
      by_stmnts[child.number_statements] << child
    end
  end
  area = 1     # Current area
  iW, iH = 1,1 # Current width and height of instance.
  pW, pH = 1,1 # Current width and height of process.
  # Ensure that these children of a group have the same area greater
  # than min_area, and the same shape if they are of the same kind.
  by_stmnts.each_key.sort.each do |num|
    ics = by_stmnts[num]
    puts "With number of statements: #{num} are base area=#{area}"
    # First compute the target area, and width and height of each type.
    ics.each do |ic|
      # Update the target area.
      n_area = ic.width * ic.height
      area = n_area if n_area > area
      # Depending on the type, update the target width and height.
      if ic.type == :instance then
        iW = ic.width if ic.width > iW
        iH = ic.height if ic.height > iH
        # Ensure the target area is reached.
        while iW*iH < area do
          # For instances, increase squarely.
          if iW <= iH then
            iW += 1
          else
            iH += 1
          end
        end
        # Reupdate the area.
        area = iW*iH
      else
        pW = ic.width if ic.width > pW
        pH = ic.height if ic.height > pH
        # Ensure the target area is reached.
        if ic.branches[0].type == :par then
          # For par processes, increase squarely.
          if pW <= pH then
            pW += 1
          else
            pH += 1
          end
        else
          # For non par processes, increase vertically.
          while pW*pH < area do
            pH += 1
          end
        end

        # Reupdate the area.
        area = pW*pH
      end
    end
    # Update the size of the ics.
    ics.each do |ic|
      if ic.type == :instance then
        ic.width = iW
        ic.height = iH
      else
        puts "For process #{ic.name} setting size from #{ic.width},#{ic.height} to #{pW},#{pH}"
        ic.width = pW
        ic.height = pH
      end
    end
    # Ensure the area increase when the number of statements is larger.
    area += 1
    puts "Now area=#{area}"
  end
end

#number_statementsObject

Give the size of the IC in number of statements.



112
113
114
115
116
117
118
119
120
121
# File 'lib/HDLRuby/hruby_viz.rb', line 112

def number_statements
  # Get the number from the branches.
  snum = @branches.reduce(0) do |sum,branch|
    sum + branch.number_statements
  end
  # Recurse on the child IC.
  snum += @children.reduce(0) do |sum,child|
    sum + child.number_statements
  end
end

#place_and_routeObject

Do the full place and route.



2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
# File 'lib/HDLRuby/hruby_viz.rb', line 2031

def place_and_route
  # Nothing to do if no children.
  if self.children.empty? then
    @route_matrix = []
    return
  end
  # Do the pre-placement.
  self.preplace_children

  puts "Pre-placement result: "
  # Display the result.
  self.children.each_with_index do |child|
    puts "#{child.name}: x=#{child.xpos} y=#{child.ypos}"
  end

  # Do the placement.
  self.place_children

  puts "Placement result: "
  # Display the result.
  self.children.each_with_index do |child|
    puts "#{child.name}: x=#{child.xpos} y=#{child.ypos}"
  end

  # Centers the result.
  self.bounding_children
  self.center_children

  # Minimize the height.
  self.bounding_children
  self.minimize_height_children

  # Centers the result.
  self.bounding_children
  self.center_children

  puts "Orientation result: "
  # Display the result.
  self.children.each do |child|
    puts "#{child.name}: x=#{child.xpos} y=#{child.ypos}"
  end

  # Compute the placement matrix.
  self.bounding_children
  matrix = self.matrix_children
  puts "matrix:"
  matrix.each { |row| puts "#{row.map{|ic| ic ? ic.name : "   " }}" }

  # Compute the side of the ports.
  self.bounding_children

  # For self.
  self.side_ports

  puts "Self ports side results:"
  self.ports.each_with_index do |port,j|
    if port.targets[0] then
      puts "#{self.name} port #{port.name} (to #{port.targets[0].ic.name}): #{port.side}"
    else
      puts "#{self.name} port #{port.name} (dangling): #{port.side}"
    end
  end

  # For the children.
  self.side_children

  puts "Children ports side results: "
  self.children.each_with_index do |child|
    child.ports.each_with_index do |port,j|
      if port.targets[0] then
        puts "#{child.name} port #{port.name} (to #{port.targets[0].ic.name}): #{port.side}"
      else
        puts "#{child.name} port #{port.name} (dangling): #{port.side}"
      end
    end
  end

  # Compute the size of the ICs.
  self.size_children
  self.normalize_size_children

  puts "Size results: "
  self.children.each do |child|
    puts "#{child.name}: width=#{child.width} height=#{child.height}"
  end

  routed = false
  @retries.times do |ret|
    puts "Route matrix sizing and route: try #{ret} (border=#{@border}, cell border=#{@cell_border})..."
    # Compute the size of the matrix cells.
    self.size_matrix
    puts "Self size: width=#{self.width} height=#{self.height}"

    puts "Cells sizes: "
    puts "  Columns' widths: #{self.cwidths}"
    puts "  Rows' heights:   #{self.rheights}"


    # Recompute the precise place of the ICs using the size of the matrix cells.
    self.place_children_matrix
    puts "Matrix-based placement result: "
    # Display the result.
    self.children.each do |child|
      puts "#{child.name}: x=#{child.xpos} y=#{child.ypos}"
    end

    # Place the ports using the matrix cells.
    self.place_ports_matrix
    puts "Matrix-base port placement result: "
    # Display the result.
    self.children.each do |child|
      puts "For #{child.name}([#{child.xpos},#{child.ypos}]):" +
        child.ports.map {|p| "#{p.name}: [#{p.side} #{p.xpos},#{p.ypos}]" }.join(", ")
    end
    puts "For self:" +
      self.ports.map {|p| "#{p.name}: [#{p.side} #{p.xpos},#{p.ypos}]" }.join(", ")

    # Fine-tune the placement.
    self.place_children_port_matrix
    puts "Matrix-base ic-port placement fine tuning result: "
    # Display the result.
    self.children.each do |child|
      puts "For #{child.name}:" +
        child.ports.map {|p| "#{p.name}: [#{p.side} #{p.xpos},#{p.ypos}]" }.join(", ")
    end
    puts "For self:" +
      self.ports.map {|p| "#{p.name}: [#{p.side} #{p.xpos},#{p.ypos}]" }.join(", ")

    # Route the ports' connections.
    routed = self.route_children
    break if routed # Success

    # @border *= 2
    # @cell_border *= 2
    # @port_width += 1
    @border *= 4
    @cell_border *= 4
    @port_width += 2
  end
  raise "Route failure." unless routed

  puts "Routes:"
  # Display the result.
  self.routes.each do |route|
    puts "#{route.ports.map {|p| p.name}.join(" to ")}:  #{route.path}"
  end

  # Generate the wiring and connection content of the tiles.
  self.wire_route_tiles

  # Compress the route matrix and update the position of the objects
  # accordignly.
  self.compress_route_tiles
end

#place_and_route_deepObject

Deeply place and route.



2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
# File 'lib/HDLRuby/hruby_viz.rb', line 2188

def place_and_route_deep
  # Place and route current IC.
  self.place_and_route
  # Recurse the place and route for the instance children.
  @children.each do |child|
    if child.type == :instance then
      # Case on instance: place and route its system.
      child.system.place_and_route_deep 
    else
      puts "Place and route node for ic=#{child.name}"
      # Otherwise recurse on the branches.
      child.branches.each { |branch| branch.place_and_route_deep }
    end
  end
end

#place_childrenObject

Place the children.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/HDLRuby/hruby_viz.rb', line 237

def place_children
  # epoch = @children.size*100
  epoch = 100
  delta = 1.0/@children.size
  epoch.times do |i|
    puts "epoch=#{i}"
    # Move the children according to the forces, 
    # # but make the first one to be on fixed position to avoid drift.
    # children[1..-1].each do |child|
    children.each do |child|
      puts "for child=#{child.name}"
      dx = delta * forceX(child,i.to_f)
      dy = delta * forceY(child,i.to_f)
      dx = 1.0 if dx > 1.0
      dx = -1.0 if dx < -1.0
      dy = 1.0 if dy > 1.0
      dy = -1.0 if dy < -1.0
      child.xpos += dx
      child.ypos += dy
      puts "Now child.xpos=#{child.xpos}"
      puts "Now child.ypos=#{child.ypos}"
    end
  end
end

#place_children_matrixObject

Tune the placing of the children according to their size and the matrix.



852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/HDLRuby/hruby_viz.rb', line 852

def place_children_matrix
  # Auth: place the children in the center of their respective
  # cells.
  ypos = 0
  @matrix.each_with_index do |row,i|
    xpos = 0
    row.each_with_index do |child,j|
      if child then
        child.xpos = xpos + (@cwidths[j]-child.width) / 2
        child.ypos = ypos + (@rheights[i]-child.height) / 2
      end
      xpos += @cwidths[j]
    end
    ypos += @rheights[i]
  end
end

#place_children_port_matrixObject

Fine-tune the placement of the ICs to increase alignment of connected ports. Also fine-place the ports of the current IC to also increase the aligment of connected ports.



1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
# File 'lib/HDLRuby/hruby_viz.rb', line 1023

def place_children_port_matrix
  # xpos, ypos = 0, 0
  # @matrix.each_with_index do |row,i|
  #   xpos = 0
  #   mypos = ypos + @rheights[i]
  #   row.each_with_index do |child,j|
  #     mxpos = xpos + @cwidths[j]
  #     if child then
  #       # Tune x position.
  #       # Find the best delta.
  #       bdx = 0       # Best y delta
  #       bcost = 1/0.0 # Best score
  #       xpos_in_cell = child.xpos - xpos  # Initial position in cell
  #       xpos_in_cell -= @border if j == 0 # Do not go inside the border
  #       (@cwidths[j]-child.width).times do |dx|
  #         dx = dx - xpos_in_cell # Adjust delta x to the initial position
  #         cost = child.ports.reduce(0) do |sum,port|
  #           pxpos = port.xpos + dx
  #           sum + port.targets.reduce(0) do |subsum,tport|
  #             # There is cost when the port is not align with the target.
  #             if (port.side == LEFT and tport.side == RIGHT) or
  #                 (port.side == UP and tport.side == UP) then
  #               # For left to right or up yo up connections, 
  #               # the first side should be 2 tiles on the left for
  #               # straight wire or avoiding routing conjestion.
  #               subsum + (pxpos-tport.xpos-2) != 0 ? 1 : 0
  #             elsif (port.side == RIGHT and tport.side == LEFT) or
  #                 (port.side == DOWN and tport.side == DOWN) then
  #               # For right to left or down to down connections,
  #               # the first side should be 2 tiles on the right for
  #               # straight wire or avoiding routing conjestion.
  #               subsum + (pxpos-tport.xpos+2) != 0 ? 1 : 0
  #             else
  #               # Otherwise, perfect aligment is the best.
  #               subsum + (pxpos-tport.xpos) != 0 ? 1 : 0
  #             end
  #           end
  #         end
  #         if cost < bcost then
  #           bcost = cost
  #           bdx = dx
  #         end
  #       end
  #       # Ensure the child does not goes out of its cell.
  #       if child.xpos + bdx < xpos then
  #         bdx = xpos - child.xpos
  #       elsif child.xpos + child.width + bdx >= mxpos then
  #         bdx = mxpos - child.xpos - child.width - 1
  #       end
  #       # Apply the best delta.
  #       child.xpos += bdx
  #       # Update the ports position.
  #       child.ports.each {|port| port.xpos += bdx }

  #       # Tune y position.
  #       puts "for child=#{child.name}"
  #       # Find the best delta.
  #       bdy = 0       # Best x delta
  #       bcost = 1/0.0 # Best score
  #       ypos_in_cell = child.ypos - ypos  # Initial position in cell
  #       ypos_in_cell -= @border if i == 0 # Do not go to the outer border
  #       (@rheights[i]-child.height).times do |dy|
  #         dy = dy - ypos_in_cell # Adjust dela y with initial position
  #         cost = child.ports.reduce(0) do |sum,port|
  #           pypos = port.ypos + dy
  #           sum + port.targets.reduce(0) do |subsum,tport|
  #             # There is cost when the port is not align with the target.
  #             if (port.side == UP and tport.side == DOWN) or
  #                 (port.side == RIGHT and tport.side == RIGHT) then
  #               # For up to down or right to right connections,
  #               # the first side should be 2 tiles on the right for
  #               # straight wire or avoiding routing conjestion.
  #               subsum + (pypos-tport.ypos+2) != 0 ? 1 : 0
  #             elsif (port.side == DOWN and tport.side == UP) or
  #                 (port.side == LEFT and tport.side == LEFT) then
  #               # For down to up or left to left connections,
  #               # the forst side should be 2 tiles on the down for
  #               # straight wire or avoiding routing conjestion.
  #               subsum + (pypos-tport.ypos-2) != 0 ? 1 : 0
  #             else
  #               subsum + (pypos-tport.ypos) != 0 ? 1 : 0 
  #             end
  #           end
  #         end
  #         if cost < bcost then
  #           bcost = cost
  #           bdy = dy
  #         end
  #       end
  #       # puts "mypos=#{mypos} child.ypos=#{child.ypos} bdy=#{bdy}"
  #       # Ensure the child does not goes out of its cell.
  #       if child.ypos + bdy < ypos then
  #         bdy = ypos - child.ypos 
  #       elsif child.ypos + child.height + bdy >= mypos then
  #         bdy = mypos - child.ypos - child.height - 1
  #       end
  #       # Apply the best delta.
  #       child.ypos += bdy
  #       # Update the ports position.
  #       child.ports.each {|port| port.ypos += bdy }
  #     end
  #     xpos += @cwidths[j]
  #   end
  #   ypos += @rheights[i]
  # end

  # First prepare the algorthims: sort the children for
  # vertical and horizontal processing and locate their
  # respective row and column intervals.
  # Sort the children by decreasing number of left and right ports, 
  # for placing the one with more freedom last.
  vert_sorted = @children.sort do |c0,c1|
    ([c1.lports.size,c1.rports.size]).max <=> 
    ([c0.lports.size,c0.rports.size]).max
  end
  hori_sorted = @children.sort do |c0,c1|
    ([c1.uports.size,c1.dports.size]).max <=> 
    ([c0.uports.size,c0.dports.size]).max
  end
  # Get the matrix row and column intervals for each children in order.
  rRange = []
  cRange = []
  xpos, ypos = 0, 0
  @matrix.each_with_index do |row,i|
    xpos = 0
    mypos = ypos + @rheights[i]
    row.each_with_index do |child,j|
      mxpos = xpos + @cwidths[j]
      if child then
        iV = vert_sorted.index(child)
        iH = hori_sorted.index(child)
        fxpos = xpos == 0 ? xpos + @border/2 : xpos + @cell_border/2
        fypos = ypos == 0 ? ypos + @border/2 : ypos + @cell_border/2
        lxpos = mxpos - @cell_border/2
        lypos = mypos - @cell_border/2
        rRange[iV] = fypos..lypos
        cRange[iH] = fxpos..lxpos
        # puts "For child=#{child.name} iV=#{iV} iH=#{iH} rRange=#{rRange[iV]} cRange=#{cRange[iH]}"
      end
      xpos += @cwidths[j]
    end
    ypos += @rheights[i]
  end

  # Tune the vertical placement of each child.
  vert_sorted.each_with_index do |child,i|
    # Tune y position.
    # puts "for child=#{child.name} child.ypos=#{child.ypos} vertical idx=#{i} rRange[i]=#{rRange[i]}"
    # Find the best delta.
    bdy = 0       # Best y delta
    bcost = 1/0.0 # Best score
    mypos = rRange[i].last
    ypos = rRange[i].first
    ypos_in_cell = child.ypos - ypos  # Initial position in cell
    # ypos_in_cell -= @border if i == 0 # Do not go to the outer border
    # (@rheights[i]-child.height).times do |dy|
    (rRange[i].size-child.height).times do |dy|
      # puts "dy=#{dy}"
      dy = dy - ypos_in_cell # Adjust detla y with initial position
      cost = child.ports.reduce(0) do |sum,port|
        pypos = port.ypos + dy
        sum + port.targets.reduce(0) do |subsum,tport|
          # There is cost when the port is not align with the target.
          if (port.side == UP and tport.side == DOWN) or
              (port.side == RIGHT and tport.side == RIGHT) then
            # For up to down or right to right connections,
            # the first side should be 2 tiles on the right for
            # straight wire or avoiding routing conjestion.
            subsum + (pypos-tport.ypos+2) != 0 ? 1 : 0
          elsif (port.side == DOWN and tport.side == UP) or
            (port.side == LEFT and tport.side == LEFT) then
            # For down to up or left to left connections,
            # the forst side should be 2 tiles on the down for
            # straight wire or avoiding routing conjestion.
            subsum + (pypos-tport.ypos-2) != 0 ? 1 : 0
          else
            subsum + (pypos-tport.ypos) != 0 ? 1 : 0 
          end
        end
      end
      if cost < bcost then
        bcost = cost
        bdy = dy
      end
    end
    # puts "mypos=#{mypos} child.ypos=#{child.ypos} bdy=#{bdy}"
    # Ensure the child does not goes out of its cell.
    if child.ypos + bdy < ypos then
      bdy = ypos - child.ypos 
    elsif child.ypos + child.height + bdy >= mypos then
      bdy = mypos - child.ypos - child.height - 1
    end
    # Apply the best delta.
    child.ypos += bdy
    # puts "child new ypos=#{child.ypos} (bdy=#{bdy})"
    # Update the ports position.
    child.ports.each {|port| port.ypos += bdy }
  end
  
  # Tune the horizontal placement of each child.
  hori_sorted.each_with_index do |child,i|
    # puts "for child=#{child.name} child.xpos=#{child.ypos} horizontal idx=#{i} cRange[i]=#{cRange[i]}"
    # Tune x position.
    # puts "for child=#{child.name}"
    # Find the best delta.
    bdx = 0       # Best x delta
    bcost = 1/0.0 # Best score
    xpos = cRange[i].first
    mxpos = cRange[i].last
    xpos_in_cell = child.xpos - xpos  # Initial position in cell
    xpos_in_cell -= @border if i == 0 # Do not go to the outer border
    # (@cwidths[i]-child.width).times do |dx|
    (cRange[i].size-child.width).times do |dx|
      dx = dx - xpos_in_cell # Adjust dela x with initial position
      cost = child.ports.reduce(0) do |sum,port|
        pxpos = port.xpos + dx
        sum + port.targets.reduce(0) do |subsum,tport|
          # There is cost when the port is not align with the target.
          if (port.side == LEFT and tport.side == RIGHT) or
              (port.side == UP and tport.side == UP) then
            # For left to right or up yo up connections, 
            # the first side should be 2 tiles on the left for
            # straight wire or avoiding routing conjestion.
            subsum + (pxpos-tport.xpos-2) != 0 ? 1 : 0
          elsif (port.side == RIGHT and tport.side == LEFT) or
            (port.side == DOWN and tport.side == DOWN) then
            # For right to left or down to down connections,
            # the first side should be 2 tiles on the right for
            # straight wire or avoiding routing conjestion.
            subsum + (pxpos-tport.xpos+2) != 0 ? 1 : 0
          else
            # Otherwise, perfect aligment is the best.
            subsum + (pxpos-tport.xpos) != 0 ? 1 : 0
          end
        end
      end
      if cost < bcost then
        bcost = cost
        bdx = dx
      end
    end
    # puts "mxpos=#{mxpos} child.xpos=#{child.ypos} bdx=#{bdx}"
    # Ensure the child does not goes out of its cell.
    if child.xpos + bdx < xpos then
      bdx = xpos - child.xpos 
    elsif child.xpos + child.width + bdx >= mxpos then
      bdx = mxpos - child.xpos - child.width - 1
    end
    # Apply the best delta.
    child.xpos += bdx
    # puts "child new xpos=#{child.xpos} (bdx=#{bdx})"
    # Update the ports position.
    child.ports.each {|port| port.xpos += bdx }
  end

  # Also place the ports of the current IC.
  poses = []
  @lports.each do |lport|
    lport.ypos = lport.targets[0].ypos
    lport.ypos += 1 if lport.targets[0].side == UP
    lport.ypos -= 1 if lport.targets[0].side == DOWN
    # Ensure ports do not overlap.
    while poses[lport.ypos] do
      lport.ypos += 1
      lport.ypos = 0 if lport.ypos >= @height
    end
    poses[lport.ypos] = lport
  end
  poses = []
  @uports.each do |uport|
    uport.xpos = uport.targets[0].xpos
    uport.xpos -= 1 if uport.targets[0].side == LEFT
    uport.xpos += 1 if uport.targets[0].side == RIGHT
    puts "Now uport.xpos=#{uport.xpos}"
    # Ensure ports do not overlap.
    while poses[uport.xpos] do
      uport.xpos += 1
      uport.xpos = 0 if uport.xpos >= @width
    end
    poses[uport.xpos] = uport
  end
  poses = []
  @rports.each do |rport|
    rport.ypos = rport.targets[0].ypos
    rport.ypos += 1 if rport.targets[0].side == UP
    rport.ypos -= 1 if rport.targets[0].side == DOWN
    # Ensure ports do not overlap.
    while poses[rport.ypos] do
      rport.ypos += 1
      rport.ypos = 0 if rport.ypos >= @height
    end
    poses[rport.ypos] = rport
  end
  poses = []
  @dports.each do |dport|
    dport.xpos = dport.targets[0].xpos
    dport.xpos -= 1 if dport.targets[0].side == LEFT
    dport.xpos += 1 if dport.targets[0].side == RIGHT
    # Ensure ports do not overlap.
    while poses[dport.xpos] do
      dport.xpos += 1
      dport.xpos = 0 if dport.xpos >= @width
    end
    poses[dport.xpos] = dport
  end
end

#place_ports_matrixObject

Compute the position of the ports of each children according to the position matrix, and also the ports of current IC.



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/HDLRuby/hruby_viz.rb', line 872

def place_ports_matrix
  # Preplace the ports, without optimization.
  (@children + [self]).each do |child|
    xpos = child.xpos
    ypos = child.ypos
    width = child.width
    height = child.height
    # The left ports.
    if child.type == :assign and child.lports.size == 1 then
      port = child.lports[0]
      # For assign types, the single ports are placed at the center.
      port.xpos = xpos
      port.ypos = ypos + height/2
    elsif child.lports.any? then
      step = child.height / child.lports.size
      child.lports.each_with_index do |port,i|
        puts "Preplace left port=#{port.name}"
        port.xpos = xpos
        port.ypos = ypos + i*step + step/2
      end
    end
    # The up ports.
    if child.type == :assign and child.uports.size == 1 then
      port = child.uports[0]
      # For assign types, the single ports are placed at the center.
      port.xpos = xpos + width/2
      port.ypos = ypos + height - 1
    elsif child.uports.any? then
      step = child.width / child.uports.size
      child.uports.each_with_index do |port,i|
        puts "Preplace up port=#{port.name}"
        port.xpos = xpos + i*step + step/2
        port.ypos = ypos + height - 1
      end
    end
    # The right ports.
    if child.type == :assign and child.rports.size == 1 then
      port = child.rports[0]
      # For assign types, the single ports are placed at the center.
      port.xpos = xpos + width - 1
      port.ypos = ypos + height/2
    elsif child.rports.any? then
      step = child.height / child.rports.size
      child.rports.each_with_index do |port,i|
        puts "Preplace right port=#{port.name}"
        port.xpos = xpos + width - 1
        port.ypos = ypos + i*step + step/2
      end
    end
    # The down ports.
    if child.type == :assign and child.dports.size == 1 then
      port = child.dports[0]
      # For assign types, the single ports are placed at the center.
      port.xpos = xpos + width/2
      port.ypos = ypos
    elsif child.dports.any? then
      step = child.width / child.dports.size
      child.dports.each_with_index do |port,i|
        puts "Preplace down port=#{port.name}"
        port.xpos = xpos + i*step + step/2
        port.ypos = ypos
      end
    end
  end

  # Optimize the place.
  @children.each do |child|
    xpos = child.xpos
    ypos = child.ypos
    width = child.width
    height = child.height

    # The left ports.
    if child.lports.size > 1 then
      step = child.height / child.lports.size
      lefts = child.lports.clone
      
      # Sort the ports by reverse order y difference with their
      # targets.
      lefts.sort! do |p0,p1| 
        p0.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.ypos - p0.ypos } <=>
        p1.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.ypos - p1.ypos }
      end
      # Apply the order.
      lefts.each_with_index do |port,i|
        port.xpos = xpos
        port.ypos = ypos + i*step + step/2
      end
    end

    # The up ports.
    if child.uports.size > 1 then
      step = child.width / child.uports.size
      ups = child.uports.clone
      
      # Sort the ports by reverse order x difference with their
      # targets.
      ups.sort! do |p0,p1| 
        p0.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.xpos - p0.xpos } <=>
        p1.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.xpos - p1.xpos }
      end
      # Apply the order.
      ups.each_with_index do |port,i|
        port.xpos = xpos + i*step + step/2
        port.ypos = ypos + height - 1
      end
    end

    # The right ports.
    if child.rports.size > 1 then
      step = child.height / child.rports.size
      rights = child.rports.clone

      # Sort the ports by reverse order y difference with their
      # targets.
      rights.sort! do |p0,p1| 
        # puts "p0=#{p0.name} p1=#{p1.name}"
        p0.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.ypos - p0.ypos } <=>
        p1.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.ypos - p1.ypos }
      end
      # Apply the order.
      rights.each_with_index do |port,i|
        port.xpos = xpos + width - 1
        port.ypos = ypos + i*step + step/2
      end
    end

    # The down ports.
    if child.dports.size > 1 then
      step = child.width / child.dports.size
      downs = child.dports.clone

      # Sort the ports by reverse order x difference with their
      # targets.
      downs.sort! do |p0,p1| 
        p0.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.xpos - p0.xpos } <=>
        p1.targets.uniq {|t| t.ic }.reduce(0) {|sum,t| sum + t.xpos - p1.xpos }
      end
      # Apply the order.
      downs.each_with_index do |port,i|
        port.xpos = xpos + i*step + step/2
        port.ypos = ypos
      end
    end
  end
end

#port?(name) ⇒ Boolean

Tell if the IC has a port named +name+

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/HDLRuby/hruby_viz.rb', line 93

def port?(name)
  name = name.to_s
  return @ports.any? {|p| p.name == name }
end

#port_str(port) ⇒ Object

Generate the string representing a port for display in the SVG



2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
# File 'lib/HDLRuby/hruby_viz.rb', line 2606

def port_str(port)
  # Generate the port name (strip everything before the last ".")
  name = port.name.sub(/^.*\./,"")
  # Strip the suffix $I and $O
  name = name.sub(/\$(I|O)$/,"")
  # Add a suffix for edge properties.
  case port.type
  when :posedge
    return name + " \u2197"
  when :negedge
    return name + " \u2198"
  else
    return name
  end
end

#preplace_childrenObject

Quickly pre-place the children in a diagonal with coordinates < 0.5



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/HDLRuby/hruby_viz.rb', line 125

def preplace_children
  # num = @children.size * 2
  # @children.each_with_index do |child,i|
  #   child.xpos = i.to_f / num
  #   child.ypos = i.to_f / num
  # end
  num = 0
  num += 1 while num*num < @children.size
  idx = 0
  num.times do |j|
    num.times do |i|
      child = @children[j*num+i]
      return unless child # The end of the preplacement.
      child.xpos = i.to_f / num
      child.ypos = j.to_f / num
    end
  end
end

#process_svg(ic) ⇒ Object

Generate a process description SVG text for +ic+



2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
# File 'lib/HDLRuby/hruby_viz.rb', line 2238

def process_svg(ic)
  id = Viz.to_svg_id(ic.name)
  res = "<rect id=\"#{id}\" fill=\"#eee\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "rx=\"#{@scale}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  # Its name.
  sy = (ic.lports.size.even? and ic.rports.size.even? ) ? 0 : -0.5 # Shift to avoid ports
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0 + sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#reconstruct_path(port0, port1, from, pos) ⇒ Object

Reconstruct the path and make the connection from +port0+ to +port1+ using the +from+ table for back tracking from position +pos+



1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
# File 'lib/HDLRuby/hruby_viz.rb', line 1581

def reconstruct_path(port0,port1,from,pos)
  # puts "reconstruct_path for #{port0} and #{port1}"
  # Create the resulting route.
  route = Route.new(port0,port1)
  # Reconstruct the path.
  path = route.path 
  path.unshift(pos)
  apos = pos
  while from.key?(pos) do
    pos = from[pos]
    if (pos == apos) then
      puts "Loop detected for path from #{port0.name} of #{port0.ic.name} to #{port1.name} of #{port1.ic.name}"
      exit
      break
    end
    apos = pos
    path.unshift(pos)
  end
  # Compute previous position: within IC since starting.
  ppos = []
  case port0.side
  when LEFT
    ppos = [pos[0]+1,pos[1]]
  when UP
    ppos = [pos[0],pos[1]-1]
  when RIGHT
    ppos = [pos[0]-1,pos[1]]
  when DOWN
    ppos = [pos[0],pos[1]+1]
  end
  # Write it into the route matrix.
  path.each do |pos|
    # Compute the direction when entering the tile (dir),
    # and the direction when leaving the previous tile (edir).
    if pos[0] > ppos[0] then
      dir = LEFT
      edir = RIGHT
    elsif pos[0] < ppos[0] then
      dir = RIGHT
      edir = LEFT
    elsif pos[1] > ppos[1] then
      dir = UP
      edir = DOWN
    else
      dir = DOWN
      edir = UP
    end
    # Update the route on the tile.
    @route_matrix[pos[1]][pos[0]].routes << [port0,port1,dir]
    # And on the previous tile.
    @route_matrix[ppos[1]][ppos[0]].routes << [port0,port1,edir]
    # Next step.
    ppos = pos
  end
  # Also return the path, it will be used for speeding up rendering.
  return route
end

#register_svg(ic) ⇒ Object

Generate a register description SVG text for +ic+



2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
# File 'lib/HDLRuby/hruby_viz.rb', line 2313

def register_svg(ic)
  id = Viz.to_svg_id(ic.name)
  res = "<rect id=\"#{id}\" fill=\"#fff\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/16.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "rx=\"#{@scale/4}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  # Its name.
  sy = (ic.lports.size <= 2 or ic.lports.size.even?) ? 0 : -0.5 # Shift to avoid ports (Note: register prots are symetrics, so check left only).
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0+sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#right_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate a right port description SVG text.



2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
# File 'lib/HDLRuby/hruby_viz.rb', line 2514

def right_port_svg(name,type,xpos,ypos,width,height)
  case type
  when :posedge
    res = "<rect fill=\"#88f\" stroke=\"#000\" " 
  when :negedge
    res = "<rect fill=\"#f88\" stroke=\"#000\" " 
  else
    res = "<rect fill=\"#ff0\" stroke=\"#000\" " 
  end
  res += "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos} #{xpos+width},#{ypos+height/2.0} " +
    "#{xpos},#{ypos+height}\"/>\n"
  return res
end

#rotate_children(t) ⇒ Object

Rotate the current placement by +t+ radiant



521
522
523
524
525
526
527
528
529
530
# File 'lib/HDLRuby/hruby_viz.rb', line 521

def rotate_children(t)
  c = Math.cos(t)
  s = Math.sin(t)
  @children.each do |child|
    xpos = child.xpos
    ypos = child.ypos
    child.xpos = xpos*c + ypos*s
    child.ypos = -xpos*s + ypos*c
  end
end

#route_childrenObject

Perform the route for the placed children. Return false in case of failure.



1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
# File 'lib/HDLRuby/hruby_viz.rb', line 1716

def route_children
  # Gather the ports to route, in case of failure the list will be
  # reordered and routing will be tried again.
  to_route = []
  @children.each  do |child|
    child.ports.each do |port|
      # Route start from output or inout.
      next if port.direction == :input 
      to_route << port
    end
  end
  # Also for current IC for routes to external, but this time using
  # input or inout as starting point.
  @ports.each do |port|
    # Route start from input or inout.
    next if port.direction == :output
    to_route << port
  end
  # Also handle the same direction output ports joined together
  # (Supported by HDLRuby).
  @ports.each do |port|
    next if port.direction != :output
    if port.targets.all? { |p| !to_route.include?(p) } then
      to_route << port
    end
  end
  # Do the routing.
  failed = nil
  retried = Hash.new {|h,k| h[k] = 0 } # The count of already retried ports.
  # StackProf.run(mode: :cpu, out: 'stackprof-output.dump') do
  # to_route.size.times do |epoch|
  (to_route.size/2).times do |epoch|
    puts "Routing epoch=#{epoch}..."
    self.init_route    # Reinitialize the route matrix.
    routed_pairs = []  # The list of already routed pair of ports.
    @routes = []       # The list of created routes.
    # Do the routing.
    failed = nil
    to_route.each.each do |p0|
      p0.targets.each do |p1|
        next if routed_pairs.include?([p0,p1])
        path = connection_route(p0,p1)
        unless path
          failed = p0
          puts "Could not route from #{p0.name} (IC: #{p0.ic.name}) to #{p1.name}(IC: #{p1.ic.name})"
          break
        end
        routed_pairs << [p0,p1]
        @routes << path
        # And for the case of inouts:
        routed_pairs << [p1,p0] if p1.direction != :input 
      end
      break if failed
    end
    if failed then
      # Was it the first port that failed?
      if to_route.first == failed then
        # Yes, no need to go on.
        break
      end
      # Put the failed port at the head of the list to route and
      # try again.
      to_route.delete(failed)
      retried[failed] += 1
      if retried[failed] > 10 then
        # The failed port has already been retried twice,
        # no need to insist.
        break
      end
      to_route.unshift(failed)
    else
      # Success.
      break
    end
  end
  # end #Stackprof
  # Failure.
  if failed then
    return false
  else
    return true
  end
end

#side_childrenObject

Select the side of the ports of the children according to the position of their targets.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/HDLRuby/hruby_viz.rb', line 330

def side_children
  @children.each do |child|
    puts "side_children for child=#{child.name} with #{child.ports.size} ports"
    # Resets the port sides.
    child.lports.clear
    child.uports.clear
    child.rports.clear
    child.dports.clear
    # Tell is a side is specifically used for input or output.
    # (Information used for certain types of IC).
    left_for, up_for, right_for, down_for = nil, nil, nil, nil
    # Recompute the ports sides.
    cx = child.xpos
    cy = child.ypos
    child.ports.each do |port|
      puts "For port: #{port.name} (of #{port.ic.name})"
      unless left_for || up_for || right_for || down_for then
        # The side is not forced.
        if port.targets.empty? then
          # Dangling port, put it on the side with the less ports.
          puts "Dangling port: #{port.name} (of #{port.ic.name})"
          pside_by_size = [[child.lports,LEFT],
                           [child.uports,UP],
                           [child.rports,RIGHT],
                           [child.dports,DOWN]].sort do |s0,s1| 
                             s0[0].size <=> s1[0].size
                           end
          port.side = pside_by_size[0][1]
          # pside_by_size[0][0] << port
        else
          # For now, use the first target only for deciding.
          # Note: if the target is the top circuit (self) the the
          # sides are reversed.
          this_port = port.targets[0].ic == self
          if this_port then
            # Current IC, do not use its position, but the side.
            case port.targets[0].side
            when LEFT
              tx = cx + 1.0
              ty = cy
            when UP
              tx = cx
              ty = -cy - 1.0
            when RIGHT
              tx = -cx - 1.0
              ty = cy
            when DOWN
              tx = cx
              ty = cy + 1.0
            end
          else
            tx = port.targets[0].ic.xpos
            ty = port.targets[0].ic.ypos
          end
          dx = cx-tx
          dy = cy-ty
          if dx > 0 then
            if dy > 0 then
              if dx > dy then
                port.side = this_port ? RIGHT : LEFT
              else
                port.side = this_port ? UP : DOWN
              end
            else
              if dx > -dy then
                port.side = this_port ? RIGHT : LEFT
              else
                port.side = this_port ? DOWN : UP
              end
            end
          else
            if dy > 0 then
              if -dx > dy then
                port.side = this_port ? LEFT : RIGHT
              else
                port.side = this_port ? UP : DOWN
              end
            else
              if -dx > -dy then
                port.side = this_port ? LEFT : RIGHT
              else
                port.side = this_port ? DOWN : UP
              end
            end
          end
        end
      end
      # Case of IC of type assign or register if a side is used for
      # output, the opposite must be used for input, and vice versa.
      if child.type == :assign or child.type == :register then
        # puts "left_for=#{left_for} up_for=#{up_for} right_for=#{right_for} down_for=#{down_for}"
        if left_for then
          if port.direction == left_for then
            port.side = LEFT
          else
            port.side = RIGHT
          end
        elsif up_for then
          if port.direction == up_for then
            port.side = UP
          else
            port.side = DOWN
          end
        elsif right_for then
          if port.direction == right_for then
            port.side = RIGHT
          else
            port.side = LEFT
          end
        elsif down_for then
          if port.direction == down_for then
            port.side = DOWN
          else
            port.side = UP
          end
        end
        # Indicate a side is decided.
        case port.side
        when LEFT
          left_for = port.direction
        when UP
          up_for = port.direction
        when RIGHT
          right_for = port.direction
        when DOWN
          down_for = port.direction
        end
      end
      puts "Chosen side=#{port.side}"
      # Update the content of the sides of the children.
      case port.side
      when LEFT
        child.lports << port
      when UP
        child.uports << port
      when RIGHT
        child.rports << port
      when DOWN
        child.dports << port
      end
    end
  end
end

#side_portsObject

Select the side of the ports of the current IC according to the position of their targets but limiting the input ports to the left or up and output ports to the right or down.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/HDLRuby/hruby_viz.rb', line 266

def side_ports
  # Resets the port sides.
  @lports.clear
  @uports.clear
  @rports.clear
  @dports.clear
  @ports.each do |port|
    if port.targets.empty? then
      puts "Dangling port: #{port.name} (of #{port.ic.name})"
      # Not connected port, put it on the side with the less ports.
      if port.direction == :input then
        if lports.size <= uports.size then
          port.side = LEFT
        else
          port.side = UP
        end
      else
        if rports.size <= dports.size then
          port.side = RIGHT
        else
          port.side = DOWN
        end
      end
    else
      # The port is connected.
      if port.direction == :input then
        # For now, use the first target only for deciding.
        d_left  = port.targets[0].ic.xpos - @box[0]
        d_up    = @box[3] - 
          port.targets[0].ic.ypos + port.targets[0].ic.height
        if d_left <= d_up then
          port.side = LEFT
        else
          port.side = UP
        end
      else
        d_right = @box[2] - 
          port.targets[0].ic.xpos + port.targets[0].ic.width
        d_down  = port.targets[0].ic.ypos - @box[1]
        if d_right <= d_down then
          port.side = RIGHT
        else
          port.side = DOWN
        end
      end
    end

    # Update the content of the sides of the children.
    case port.side
    when LEFT
      @lports << port
    when UP
      @uports << port
    when RIGHT
      @rports << port
    when DOWN
      @dports << port
    end
  end
end

#size_childrenObject

Compute the width and height in number of routes for each children in current placement and ports position.



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/HDLRuby/hruby_viz.rb', line 670

def size_children
  @children.each do |child|
    nl = child.lports.size * @port_width
    nu = child.uports.size * @port_width
    nr = child.rports.size * @port_width
    nd = child.dports.size * @port_width
    child.height = nl > nr ? nl : nr
    child.width  = nu > nd ? nu : nd
    puts "First width=#{child.width} height=#{child.height} [#{child.lports.size},#{child.uports.size},#{child.rports.size},#{child.dports.size}]"
    # Ensure IC have some thickness.
    if child.type != :register then
      # For general IC.
      # First at least one-port wide.
      child.height = @port_width if child.height < @port_width
      child.width = @port_width if child.width < @port_width
      # But enlarge if more than one port horizontally or
      # vertically for easier routing and readability.
      if child.lports.size + child.rports.size > 1 and
          child.height == @port_width and
          child.width == @port_width then
        child.width *= 2
      end
      if child.uports.size + child.dports.size > 1 and 
          child.width == @port_width and
          child.height == @port_width then
        child.height *= 2
      end
      # Also ensure the chip is wide enough.
      if child.type == :assign then
        # For an ALU case.
        if child.lports.size > 0 and child.height < 5 then
          child.height = 5
        end
        if child.uports.size > 0 and child.width < 5 then
          child.width = 5
        end
      elsif child.type == :memory then
        # For a memory case: it is square (it is a matrix).
        if child.width > child.height then
          child.height = child.width
        else
          child.width = child.height
        end
      else
        # For the other cases.
        if child.height + child.width < (@port_width+1)*2 then
          child.height += @port_width
        # Also enlarge if area too small.
        end
      end
    else
      # Register are thinner in their representation
      if child.ports.size <= 2 then
        # if they have no sub ports very thin.
        child.height = 2 if child.height < @port_width
        child.width = 2 if child.width < @port_width
      else
        # Otherwise wide enough to write the sub ports.
        child.height = 3 if child.height < @port_width
        child.width = 3 if child.width < @port_width
      end
      puts "for register: #{child.name} ports.size=#{child.ports.size}, width=#{child.width} height=#{child.height}"
    end

  end
end

#size_matrixObject

Compute the sizes of the matrix cells in number of routes, then update the size of current IC for matching the matrix.



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/HDLRuby/hruby_viz.rb', line 821

def size_matrix
  # Compute the sizes that fits perfectly the children.
  @rheights = [ 0 ] * @matrix.size
  @cwidths = [ 0 ] * @matrix[0].size
  @matrix.each_with_index do |row,i|
    row.each_with_index do |child,j|
      next unless child
      rh = @rheights[i]
      cw = @cwidths[j]
      @cwidths[j]  = child.width if child.width > cw
      @rheights[i] = child.height if child.height > rh
    end
  end 
  # Increase the space to allow placing the routes.
  nrows = @matrix.size
  ncols = @matrix[0].size
  # @rheights.map! {|h| h + nrows*@cell_border*2 }
  @rheights.map! {|h| h + @cell_border*2 }
  @rheights[0] += @border
  @rheights[-1] += @border
  # @cwidths.map!  {|w| w + ncols*@cell_border*2 }
  @cwidths.map!  {|w| w + @cell_border*2 }
  @cwidths[0] += @border
  @cwidths[-1] += @border
  # Update the size of the current IC.
  @width = @cwidths.reduce(:+) 
  @height = @rheights.reduce(:+)
end

#sub_port?(port) ⇒ Boolean

Tell if a port is a sub-port of a register.

Returns:

  • (Boolean)


2601
2602
2603
# File 'lib/HDLRuby/hruby_viz.rb', line 2601

def sub_port?(port)
  return !port.name.sub(self.name,"").sub(/\$(I|O)/,"").empty?
end

#system_svg(ic) ⇒ Object

Generate a system description SVG text for +ic+



2206
2207
2208
2209
2210
2211
2212
# File 'lib/HDLRuby/hruby_viz.rb', line 2206

def system_svg(ic)
  return "<rect fill=\"#fff\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/8.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
end

#taxi_distance(pos0, pos1) ⇒ Object

Compute the taxi cab distance between +pos0+ and +pos1+



1503
1504
1505
# File 'lib/HDLRuby/hruby_viz.rb', line 1503

def taxi_distance(pos0,pos1)
  return (pos0[0] - pos1[0]).abs + (pos0[1] - pos1[1]).abs
end

#timed_process_svg(ic) ⇒ Object

Generate a timed process description SVG text for +ic+



2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
# File 'lib/HDLRuby/hruby_viz.rb', line 2290

def timed_process_svg(ic)
  id = Viz.to_svg_id(ic.name)
  res = "<rect id=\"#{id}\" fill=\"#bbb\" stroke=\"#000\" " +
    "stroke-width=\"#{@scale/8.0}\" " +
    "x=\"#{ic.xpos*@scale}\" y=\"#{ic.ypos*@scale}\" " +
    "rx=\"#{@scale}\" " +
    "width=\"#{ic.width*@scale}\" "+
    "height=\"#{ic.height*@scale}\"/>\n"
  # Its name.
  sy = (ic.lports.size.even? and ic.rports.size.even? ) ? 0 : -0.5 # Shift to avoid ports
  res += "<text id=\"text#{id}\" " +
    "style=\"text-anchor: middle; dominant-baseline: middle;\" " +
    "font-family=\"monospace\" font-size=\"1px\" " +
    "x=\"#{(ic.xpos + ic.width/2.0)*@scale}\" "+
    "y=\"#{(ic.ypos + ic.height/2.0 + sy)*@scale}\">" +
    ic.name + "</text>\n"
  # Its text resizing.
  res += Viz.svg_text_fit("text#{id}",(ic.width-0.6)*@scale,
                           0.6*@scale)
  return res
end

#to_svg(top = true, tx = 0, ty = 0, width = nil, height = nil) ⇒ Object

Generate in SVG format the graphical representation of the IC. +top+ tells if it is the top IC. +tx+ is the x translation of the full description. +ty+ is the y translation of the full description. +width+ is the forced width of the full description if any. +height+ is the forced height of the full description if any.



2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
# File 'lib/HDLRuby/hruby_viz.rb', line 2631

def to_svg(top = true, tx=0, ty=0, width=nil, height=nil)
  # Compute the various sizes.
  x0,y0, x1,y1 = 0,0, @width*@scale, @height*@scale
  puts "x0,y0, x1,y1 = #{x0},#{y0}, #{x1}, #{y1}"
  bT = (@scale * 1.5)                # Border thickness
  pT = (bT / 5.0)                    # Port thickness
  wT = bT / 30.0                     # Wire thickness
  sF = @scale*0.4                    # Small font height
  mF = @scale*0.6                    # Medium font height
  lF = @scale*1.0                    # Large font height
  width  = (x1-x0)+bT*5 unless width
  height = (y1-y0)+bT*5 unless height
  stx = (width - (x1-x0)) / 2.0 # X translation of the top system
  sty = (height - (y1-y0)) / 2.0 # Y translation of the top system
  puts "bT=#{bT} pT=#{pT} wT=#{wT} width=#{width} height=#{height}"
  # The initial visibility.
  visibility = top ? "visible" : "hidden"
  # The string used as suffix for style class names.
  @idC = "-" + self.name.gsub(/[:$]/,"-")
  # Generate the SVG code.
  if top then
    # It is the top IC.
    # Generate the header.
    res = Viz.header_svg(self.name,x0,y0,width,height)
  else
    # It is not the top, no need of initialization.
    res = ""
  end

  # Sets the styles.
  res += "<style>\n"
  # Fonts
  res += ".small#{self.idC}  { font: #{sF}px sans-serif; }\n"
  res += ".medium#{self.idC} { font: #{mF}px sans-serif; }\n"
  res += ".large#{self.idC}  { font: #{lF}px sans-serif; }\n"
  res += "</style>\n"

  # Generate the group containing all the IC description.
  res += "<g id=\"#{self.name}\" visibility=\"#{visibility}\" " +
         "transform=\"translate(#{tx},#{ty})\">\n"
  # Generate the rectangle of the bounding box.
  res += "<rect fill=\"#bbb\" stroke=\"#555\" " +
    "stroke-width=\"#{@scale/4.0}\" " +
    # "x=\"#{x0-bT*2.5}\" y=\"#{y0-bT*2.5}\" "+
    "x=\"#{x0}\" y=\"#{y0}\" "+
    "width=\"#{width}\" height=\"#{height}\"/>\n"

  # Generate the group containing the top system and its contents.
  res += "<g transform=\"translate(#{stx},#{sty})\">\n"

  # Generate current IC's box.
  # The SVG object representing a system.
  res += system_svg(self)

  # Draw the routes and connection points.
  @route_matrix.each_with_index do |row,y|
    row.each_with_index do |tile,x|
      # # Draw the tiles contour for debug.
      # res += "<rect fill=\"none\" stroke=\"#00F\" " +
      #   "x=\"#{x*@scale}\" " +
      #   "y=\"#{y*@scale}\" " +
      #   "width=\"#{@scale}\" height=\"#{@scale}\"/>\n"
      #
      # Draw the wires.
      tile.wires.each do |wire_dir|
        # puts "wire_dir=#{wire_dir}"
        # Draw the wire (as a thin rectangle).
        case wire_dir
        when LEFT|RIGHT
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x)*@scale}\" " +
            "y1=\"#{(y+0.5)*@scale}\" " +
            "x2=\"#{(x+1)*@scale}\" " +
            "y2=\"#{(y+0.5)*@scale}\" />\n"
        when RIGHT|DOWN
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x+1)*@scale}\" " +
            "y1=\"#{(y+0.5)*@scale}\" " +
            "x2=\"#{(x+0.5)*@scale}\" " +
            "y2=\"#{(y)*@scale}\" />\n"
        when RIGHT|UP
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x+1)*@scale}\" " +
            "y1=\"#{(y+0.5)*@scale}\" " +
            "x2=\"#{(x+0.5)*@scale}\" " +
            "y2=\"#{(y+1)*@scale}\" />\n"
        when LEFT|DOWN
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x+0.5)*@scale}\" " +
            "y1=\"#{(y)*@scale}\" " +
            "x2=\"#{(x)*@scale}\" " +
            "y2=\"#{(y+0.5)*@scale}\" />\n"
        when LEFT|UP
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x+0.5)*@scale}\" " +
            "y1=\"#{(y+1)*@scale}\" " +
            "x2=\"#{(x)*@scale}\" " +
            "y2=\"#{(y+0.5)*@scale}\" />\n"
        when UP|DOWN
          res += "<line stroke=\"#000\" stroke-width=\"#{wT}\" " +
            "stroke-linecap=\"round\" " +
            "x1=\"#{(x+0.5)*@scale}\" " +
            "y1=\"#{(y)*@scale}\" " +
            "x2=\"#{(x+0.5)*@scale}\" " +
            "y2=\"#{(y+1)*@scale}\" />\n"
        end
      end

      # Draw the connection points.
      tile.dots.each do |dot_pos|
        res += "<rect fill=\"#000\" stroke=\"#000\" " +
               "stroke-width=\"#{wT}\" "
        case dot_pos
        when LEFT
          res += "x=\"#{(x+0.0)*@scale-wT*2}\" " + 
            "y=\"#{(y+0.5)*@scale-wT*2}\" "
        when UP
          res += "x=\"#{(x+0.5)*@scale-wT*2}\" " + 
            "y=\"#{(y+1.0)*@scale-wT*2}\" "
        when RIGHT
          res += "x=\"#{(x+1.0)*@scale-wT*2}\" " + 
            "y=\"#{(y+0.5)*@scale-wT*2}\" "
        when DOWN
          res += "x=\"#{(x+0.5)*@scale-wT*2}\" " + 
            "y=\"#{(y+0.0)*@scale-wT*2}\" "
        end
        res += "width=\"#{wT*4}\" height=\"#{wT*4}\"/>\n"
      end
    end
  end

  # Generate the children boxes.
  @children.each do |child|
    case child.type
    when :assign
      # The SVG object representing an ALU is to draw.
      res += alu_svg(child)
    when :process
      # The SVG object representing a process is to draw.
      res += process_svg(child)
    when :clocked_process
      # The SVG object representing a cloked process is to draw.
      res += clocked_process_svg(child)
    when :timed_process
      # The SVG object representing a timed process is to draw.
      res += timed_process_svg(child)
    when :register
      # The SVG object representing a register is to draw.
      res += register_svg(child)
    when :memory
      # The SVG object representing a memory is to draw.
      res += memory_svg(child)
    else
      # The SVG object representing an instance is to draw.
      res += instance_svg(child)
    end
  end

  # Generate the port boxes.
  ([ self ] + @children).each do |child|
    # puts "Drawing port for: #{child.name}"
    # Left ports
    child.lports.each_with_index do |port|
      # Draw the port symbol.
      case port.direction
      when :input
        res += self.right_port_svg(port.name,port.type,
                                   port.xpos*@scale-pT/2,
                                   (port.ypos+0.5)*@scale-pT/2,pT,pT)
      when :output
        res += self.left_port_svg(port.name,port.type,
                                  port.xpos*@scale-pT/2,
                                  (port.ypos+0.5)*@scale-pT/2,pT,pT)
      when :inout
        res += self.left_right_port_svg(port.name,port.type,
                                        port.xpos*@scale-pT/2,
                                        (port.ypos+0.5)*@scale-pT/2,pT,pT)
      end
      # And set its name if it is not a register, memory non-sub port,
      # or an undirected port.
      if (child.type != :register and child.type != :memory and
          port.direction != :none) or child.sub_port?(port) then
        if child == self then
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: end\" " +
            "x=\"#{(port.xpos)*@scale-pT}\" "+
            "y=\"#{(port.ypos+0.5)*@scale+sF/2.5}\">" + # port.name +
            self.port_str(port) + "</text>\n"
        else
          res += "<text class=\"small#{self.idC}\" x=\"#{(port.xpos)*@scale+pT}\" "+
            "y=\"#{(port.ypos+0.5)*@scale+sF/2.5}\">" + # port.name + 
            self.port_str(port) + "</text>\n"
        end
      end
    end
    # Up ports.
    child.uports.each_with_index do |port|
      # puts "uport: #{port.name} xpos=#{port.xpos} ypos=#{port.ypos}"
      case port.direction
      when :input
        res += self.down_port_svg(port.name,port.type,
                                  (port.xpos+0.5)*@scale-pT/2,
                                  (port.ypos+1)*scale-pT/2,pT,pT)
      when :output
        res += self.up_port_svg(port.name,port.type,
                                (port.xpos+0.5)*@scale-pT/2,
                                (port.ypos+1)*scale-pT/2,pT,pT)
      when :inout
        res += self.up_down_port_svg(port.name,port.type,
                                     (port.xpos+0.5)*@scale-pT/2,
                                     (port.ypos+1)*scale-pT/2,pT,pT)
      end
      # And set its name if it is not a register, memory non sub-port,
      # or an undirected port.
      if (child.type != :register and child.type != :memory and
          port.direction != :none) or child.sub_port?(port) then
        if child == self then
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: middle\" " +
            "x=\"#{(port.xpos+0.5)*@scale}\" "+
            "y=\"#{(port.ypos+1.0)*@scale+pT+sF/2}\">" + # port.name + 
            self.port_str(port) + "</text>\n"
        else
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: middle\" " +
            "x=\"#{(port.xpos+0.5)*@scale}\" "+
            "y=\"#{(port.ypos+1.0)*@scale-pT}\">" + # port.name +
            self.port_str(port) + "</text>\n"
        end
      end
    end
    # Right ports
    child.rports.each_with_index do |port|
      case port.direction
      when :input
        res += self.left_port_svg(port.name,port.type,
                                  (port.xpos+1)*@scale-pT/2,
                                  (port.ypos+0.5)*scale-pT/2,pT,pT)
      when :output
        res += self.right_port_svg(port.name,port.type,
                                   (port.xpos+1)*@scale-pT/2,
                                   (port.ypos+0.5)*scale-pT/2,pT,pT)
      when :inout
        res += self.left_right_port_svg(port.name,port.type,
                                        (port.xpos+1)*@scale-pT/2,
                                        (port.ypos+0.5)*scale-pT/2,pT,pT)
      end
      # And set its name if it is not a register, memory non sub-port,
      # or an undirected port.
      if (child.type != :register and child.type != :memory and
          port.direction != :none) or child.sub_port?(port) then
        if child == self then
          res += "<text class=\"small#{self.idC}\" " +
            "x=\"#{(port.xpos+1)*@scale+pT}\" " +
            "y=\"#{(port.ypos+0.5)*@scale+sF/2.5}\">" + # port.name +
            self.port_str(port) + "</text>\n"
        else
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: end\" " +
            "x=\"#{(port.xpos+1)*@scale-pT}\" "+
            "y=\"#{(port.ypos+0.5)*@scale+sF/2.5}\">" + # port.name + 
            self.port_str(port) + "</text>\n"
        end
      end
    end
    # Down ports.
    child.dports.each_with_index do |port|
      case port.direction
      when :input
        res += self.up_port_svg(port.name,port.type,
                                (port.xpos+0.5)*@scale-pT/2,
                                port.ypos*scale-pT/2,pT,pT)
      when :output
        res += self.down_port_svg(port.name,port.type,
                                  (port.xpos+0.5)*@scale-pT/2,
                                  port.ypos*scale-pT/2,pT,pT)
      when :inout
        res += self.up_down_port_svg(port.name,port.type,
                                     (port.xpos+0.5)*@scale-pT/2,
                                     port.ypos*scale-pT/2,pT,pT)
      end
      # And set its name if it is not a register, memory non sub-port,
      # or an undirected port.
      if (child.type != :register and child.type != :memory and
          port.direction != :none) or child.sub_port?(port) then
        if child == self then
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: middle\" " +
            "x=\"#{(port.xpos+0.5)*@scale}\" "+
            "y=\"#{(port.ypos)*@scale-pT}\">" + # port.name + 
            self.port_str(port) + "</text>\n"
        else
          res += "<text class=\"small#{self.idC}\" style=\"text-anchor: middle\" " +
            "x=\"#{(port.xpos+0.5)*@scale}\" "+
            # "y=\"#{(port.ypos)*@scale+pT+sF}\">" + # port.name + 
            "y=\"#{(port.ypos)*@scale+pT+sF/2.0}\">" + # port.name + 
            self.port_str(port) + "</text>\n"
        end
      end
    end
  end

  # Generate the children's inside if any.
  (@children+@branches).each do |child|
    target = nil
    # Determine the target element to represent.
    case child.type
    when :instance
      target = child.system
    when :assign, :process, :clocked_process, :timed_process
      target = child.branches[0]
    else
      target = child if child.is_a?(Node)
    end
    next unless target # No target? Skip.
    # Compute the child display width.
    cwidth = child.width-pT/@scale
    cheight = child.height-pT/@scale
    # Translate inside the instance.
    ctx = child.xpos + pT/(@scale*2.0)
    cty = child.ypos + pT/(@scale*2.0)
    # For the case of an assign IC (alu), reduce and tranlate a bit
    # on longer side, to accomodate the diagonal borders.
    if child.type == :assign then
      if child.lports.any? then
        cheight -= 2
        cty += 1
      else
        cwidth -= 2
        ctx += 1
      end
    end
    # For the case of a process, reduce and translate a bit on
    # the largest side to accomodate the round borders and usually long
    # dataflow represented.
    if [:process, 
        :clocked_process, :timed_process].include?(child.type) then
      if cwidth >= cheight then
        cwidth -= 2
        ctx += 1
      else
        cheight -= 2
        cty += 1
      end
    end
    # Leave a space for left or right ports if any.
    sl = child.ports.any? {|p| p.side == LEFT } ? 1.0 : 0.0
    sr = child.ports.any? {|p| p.side == RIGHT } ? 1.0 : 0.0
    # Recompute the scale.
    fit = [
      (target.width+sl+sr+(bT/@scale)) / (cwidth),
      (target.height+(bT/@scale)) / (cheight),
      3.0
    ].max
    target.scale = @scale / fit
    puts "fit=#{fit} target.scale=#{target.scale}"
    puts "child.xpos=#{child.xpos} child.ypos=#{child.ypos} ctx=#{ctx} cty=#{cty}"
    res += target.to_svg(false,ctx*@scale,cty*@scale,
                         cwidth*scale,cheight*scale)
    # Generate the closing button element.
    res += Viz.closing_svg(target.name + '_close',pT,(ctx+cwidth)*@scale-pT,cty*@scale)
  end

  # Close the group containing the top system and its content.
  res += "</g>\n"
  # Close the group containing the description of the IC.
  res += "</g>\n"

  # Generate the scripts for controlling the appearance of the
  # children' and contents' inside.
  (@children+@branches).each do |child|
    target = nil
    # Determine the target element to represent.
    case child.type
    when :instance
      target = child.system
    when :assign, :process, :clocked_process, :timed_process
      target = child.branches[0]
    else
      target = child if child.is_a?(Node)
    end
    next unless target # No target? Skip.
    res += <<~SCRIPT
<script>
diagram.getElementById('#{child.name}').addEventListener("click", (e) => {
// For the element.
let elem = diagram.getElementById('#{target.name}');
elem.setAttribute('visibility','visible');
// And its closing button.
elem = diagram.getElementById('#{target.name}_close');
elem.setAttribute('visibility','visible');
});
diagram.getElementById('#{target.name}_close').addEventListener("click", (e) => {
// For the element.
let elem = diagram.getElementById('#{target.name}');
elem.setAttribute('visibility','hidden');
// And its closing button.
elem = diagram.getElementById('#{target.name}_close');
elem.setAttribute('visibility','hidden');
});

</script>
      SCRIPT
  end

  if top then
    # It is the top so generate the help panel and close the SVG.
    res += Viz.help_svg(x0,y0,width,height)
    res += "</svg>\n"
  end
  return res
end

#touch?(pos0, pos1) ⇒ Boolean

Tell if two positions touch each other (diagonal touch is not considered valid).

Returns:

  • (Boolean)


1641
1642
1643
1644
# File 'lib/HDLRuby/hruby_viz.rb', line 1641

def touch?(pos0,pos1)
  return (((pos0[0]-pos1[0]).abs < 2 and pos0[1] == pos1[1]) or
          ((pos0[1]-pos1[1]).abs < 2 and pos0[0] == pos1[0]))
end

#up_down_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate an up-down port description SVG text.



2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
# File 'lib/HDLRuby/hruby_viz.rb', line 2586

def up_down_port_svg(name,type,xpos,ypos,width,height)
  res = "<rect fill=\"#FF0\" stroke=\"#000\" " +
    "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos} #{xpos+width/2},#{ypos+height} " +
    "#{xpos+width},#{ypos}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos+width/2.0},#{ypos} #{xpos},#{ypos+height} " +
    "#{xpos+width},#{ypos+height}\"/>\n"
  return res
end

#up_port_svg(name, type, xpos, ypos, width, height) ⇒ Object

Generate an up port description SVG text.



2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
# File 'lib/HDLRuby/hruby_viz.rb', line 2548

def up_port_svg(name,type,xpos,ypos,width,height)
  case type
  when :posedge
    res = "<rect fill=\"#88f\" stroke=\"#000\" " 
  when :negedge
    res = "<rect fill=\"#f88\" stroke=\"#000\" " 
  else
    res = "<rect fill=\"#ff0\" stroke=\"#000\" " 
  end
  res += "x=\"#{xpos}\" y=\"#{ypos}\" " + 
    "stroke-width=\"#{@scale/16.0}\" " +
    "width=\"#{width}\" height=\"#{height}\"/>\n"
  res += "<polygon fill=\"#000\" stroke=\"none\" " +
    "points=\"#{xpos},#{ypos} #{xpos+width/2.0},#{ypos+height} " +
    "#{xpos+width},#{ypos}\"/>\n"
  return res
end

#wire_route_tilesObject

Generate the wiring and connection content of the tiles.



1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
# File 'lib/HDLRuby/hruby_viz.rb', line 1802

def wire_route_tiles
  # The table of route segments per tiles, used for determining the
  # connection points.
  tile_segs = Hash.new {|h,k| h[k] = [] }

  # Wire the routes.
  @routes.each do |route|
    port0 = route.ports[0] # The routed start port.
    port1 = route.ports[1] # The routed end port.
    # Add the start and end port position to the route.
    sport,eport = *route.ports
    if sport.ic != self then
      route.path.unshift([sport.xpos,sport.ypos])
    else
      case sport.side
      when LEFT
        route.path.unshift([sport.xpos-1,sport.ypos])
      when UP
        route.path.unshift([sport.xpos,sport.ypos+1])
      when RIGHT
        route.path.unshift([sport.xpos+1,sport.ypos])
      when DOWN
        route.path.unshift([sport.xpos,sport.ypos-1])
      end
    end
    if eport.ic != self then
      route.path.append([eport.xpos,eport.ypos])
    else
      case eport.side
      when LEFT
        route.path.append([eport.xpos-1,eport.ypos])
      when UP
        route.path.append([eport.xpos,eport.ypos+1])
      when RIGHT
        route.path.append([eport.xpos+1,eport.ypos])
      when DOWN
        route.path.append([eport.xpos,eport.ypos-1])
      end
    end
    route.path.each_cons(3).with_index do |((x0,y0),(x1,y1),(x2,y2)),i|
      # puts "x0,y0=#{x0},#{y0} x1,y1=#{x1},#{y1} x2,y2=#{x2},#{y2}"
      # puts "i=#{i} dir=#{dir}"
      dir = 0
      dir |= RIGHT if x0 > x1 || x1 < x2
      dir |= LEFT  if x0 < x1 || x1 > x2
      dir |= UP    if y0 > y1 || y1 < y2
      dir |= DOWN  if y0 < y1 || y1 > y2
      # Set the wire.
      @route_matrix[y1][x1].wires << dir
      # Update the list of segments. It will be used later for 
      # determining the connection points.
      # NOTE: if it is a head or tail of route, there is no dir.
      tile_segs[[x1,y1]] << dir if dir
    end
  end

  # Generate the connection points.
  tile_segs.each do |(x,y),segs|
    # Keep one uniq element per undirected direction.
    # segs = segs.map { |dir| UNDIRECTED[dir] }.uniq
    segs = segs.uniq
    # Determin the fork position if any.
    puts "segs=#{segs}" if segs.size > 1
    fork_pos = nil
    if    (segs & [ LEFT|RIGHT, LEFT|UP]).size == 2 then
      fork_pos = LEFT
    elsif (segs & [ LEFT|RIGHT, LEFT|DOWN]).size == 2 then
      fork_pos = LEFT
    elsif (segs & [ LEFT|UP, LEFT|DOWN]).size == 2 then
      fork_pos = LEFT
    elsif (segs & [ UP|DOWN, UP|LEFT]).size == 2 then
      fork_pos = UP
    elsif (segs & [ UP|DOWN, UP|RIGHT]).size == 2 then
      fork_pos = UP
    elsif (segs & [ UP|LEFT, UP|RIGHT]).size == 2 then
      fork_pos = UP
    elsif (segs & [ RIGHT|LEFT, RIGHT|UP]).size == 2 then
      fork_pos = RIGHT
    elsif (segs & [ RIGHT|LEFT, RIGHT|DOWN]).size == 2 then
      fork_pos = RIGHT
    elsif (segs & [ RIGHT|UP, RIGHT|DOWN]).size == 2 then
      fork_pos = RIGHT
    elsif (segs & [ DOWN|UP, DOWN|LEFT]).size == 2 then
      fork_pos = DOWN
    elsif (segs & [ DOWN|UP, DOWN|RIGHT]).size == 2 then
      fork_pos = DOWN
    elsif (segs & [ DOWN|LEFT, DOWN|RIGHT]).size == 2 then
      fork_pos = DOWN
    end
    if fork_pos then
      puts "x=#{x} y=#{y} segs=#{segs} fork_pos=#{fork_pos}"
      # Set the connection point.
      @route_matrix[y][x].dots << fork_pos
    end
  end
end

#xrate(ic0, ic1) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/HDLRuby/hruby_viz.rb', line 161

def xrate(ic0,ic1)
  d = distance(ic0,ic1)
  # Ensure d is not 0 to avoid infinity.
  d = EPSILON if d < EPSILON
  res = (ic0.xpos-ic1.xpos)/d
  # puts "ic0.xpos=#{ic0.xpos} ic1.xpos=#{ic1.xpos} xrate=#{res}"
  return res
end

#yrate(ic0, ic1) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/HDLRuby/hruby_viz.rb', line 170

def yrate(ic0,ic1)
  d = distance(ic0,ic1)
  # Ensure d is not 0 to avoid infinity.
  d = EPSILON if d < EPSILON
  res = (ic0.ypos-ic1.ypos)/d
  return res
end