Skip to content

Commit

Permalink
add Block Behavior Process Component classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cordoval committed Apr 29, 2012
1 parent 44a1f12 commit f1f0480
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 87 deletions.
79 changes: 0 additions & 79 deletions lib/computer_build/vhdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,6 @@ def self.STD_LOGIC_VECTOR(range)
end
end

module StatementBlock
def case(input, &body)
@statements << Case.new(input, body)
end

def if(*conditions, &body)
ifthenelse = If.new(conditions, body)
@statements << ifthenelse
return ifthenelse
end

def assign(*args)
@statements << Assignment.new(*args)
end

def high(target)
assign(target, '1')
end

def low(target)
assign(target, '0')
end

# Default generate, generally overridden
def generate(out, indent)
@statements.each {|s| s.generate(out, indent + 1)}
end
end

class SingleLineStatement < Statement
def generate(out, indent)
out.print " " * indent
out.print self.line()
out.print "\n"
end
end

class Entity
attr_reader :name
def initialize(name, body)
Expand Down Expand Up @@ -96,40 +59,6 @@ def generate(out=$stdout)
end
end

class Behavior
include StatementBlock

def initialize(body)
@statements = []
body.call(self)
end

def process(inputs, &body)
@statements << VHDL::Process.new(inputs, body)
end

def instance(*args)
@statements << Instance.new(*args)
end
end

class Process
include StatementBlock
def initialize(inputs, body)
@inputs = inputs
@statements = []
body[self]
end

def generate(out, indent)
prefix = " " * indent
args = @inputs.map(&:to_s).join(',')
out.puts prefix + "PROCESS(#{args})"
out.puts prefix + "BEGIN"
@statements.each {|s| s.generate(out, indent + 1)}
out.puts prefix + "END PROCESS;"
end
end

class Case
def initialize(input, body)
Expand Down Expand Up @@ -220,14 +149,6 @@ def generate(out, indent)
end
end

class Block < MultiLineStatement
include StatementBlock

def initialize(body)
@statements = []
body.call(self)
end
end

# Global scope methods for creating stuff

Expand Down
26 changes: 26 additions & 0 deletions src/ComputerBuild/Vhdl/Behavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ComputerBuild\Vhdl;

class Behavior
{
use StatementTrait;

protected $statements;

public function __construct($body)
{
$this->statements = array();
$body->call($this);
}

public function process($inputs, $body)
{
return $this->statements[] = new Process($inputs, $body);
}

public function instance($args = null)
{
return $this->statements[] = new Instance($args = null);
}
}
17 changes: 17 additions & 0 deletions src/ComputerBuild/Vhdl/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ComputerBuild\Vhdl;

class Block extends MultiLineStatement
{
use StatementBlock;

protected $statements;

public function __construct($body)
{
$this->statements = array();
$body->call($this);
}

}
8 changes: 4 additions & 4 deletions src/ComputerBuild/Vhdl/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ public function __construct($name, $injectedBlock)

public function in($name, $type)
{
$this->ports = new Port($name, "in", $type);
$this->ports[] = new Port($name, "in", $type);
}

public function out($name, $type)
{
$this->ports = new Port($name, "out", $type);
$this->ports[] = new Port($name, "out", $type);
}

public function inout($name, $type)
{
$this->ports = new Port($name, "inout", $type);
$this->ports[] = new Port($name, "inout", $type);
}

public function generate($out, $indent)
{
$prefix = str_pad('', $ident, " ");
$prefix = str_pad('', $indent, " ");
$out->print($prefix."COMPONENT ".$this->name);
$out->print($prefix."PORT(");
foreach ($this->ports as $index => $port) {
Expand Down
28 changes: 28 additions & 0 deletions src/ComputerBuild/Vhdl/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ComputerBuild\Vhdl;

class Process
{
use StatementTrait;

protected $statements;
protected $inputs;

public function __construct($inputs, $body)
{
$this->inputs = $inputs;
$this->statements = array();
$body[$this];
}

public function generate($out, $indent)
{
$prefix = str_pad('', $indent, " ");
$args = $this->inputs->map(&:to_s).join(',');
$out->print($prefix."PROCESS(".$args.")");
$out->print($prefix."BEGIN");
//$this->statements.each {|s| s.generate(out, indent + 1)}
$out->print($prefix."END PROCESS;");
}
}
7 changes: 3 additions & 4 deletions src/ComputerBuild/Vhdl/SingleLineStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ class SingleLineStatement extends Statement
{
public function generate($out, $indent)
{
for ($i = 0; $i <= $indent; $i++) {
$out->print(" ");
}
$out->print(parent::line());
$prefix = str_pad('', $indent, " ");
$out->print($prefix);
$out->print($this->line());
$out->print("\n");
}
}
41 changes: 41 additions & 0 deletions src/ComputerBuild/Vhdl/StatementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace ComputerBuild\Vhdl;

trait StatementTrait
{
protected $statements;

public function caseVhdl($input, $body)
{
return $this->statements[] = new CaseVhdl($input, $body);
}

public function ifVhdl($conditions, $body)
{
$ifThenElse = new IfVhdl($conditions, $body);
$this->statements[] = $ifThenElse;
return $ifThenElse;
}

public function assign($args = null)
{
return $this->statements[] = new Assignment($args = null);
}

public function high($target)
{
return new Assign($target, 1);
}

public function low($target)
{
return new Assign($target, 0);
}

public function generate($out, $indent)
{
//$this->statements
//@statements.each {|s| s.generate(out, indent + 1)}
}
}

0 comments on commit f1f0480

Please sign in to comment.