6. すごくべたなプログラムにみるコード生成の例
べたなプログラムを用意
public class SimpleProgram{
public int sum(int a, int b, int c,public int sum(int a, int b, int c,
int d, int e, int f,
int g, int h, int i,
int j, int k, int l){
c = a + b;
f = d + e;
i = g + h;
l = j + k;
c = c + f;
6
c = c + f;
l = l + i;
c = c + l;
return c;
}
}
14. インスタンス生成
クラスのインスタンスを生成/利用できる
public class Test002 {
public static final int DEFAULT_VALUE = 0x20;
ex. sample/test/Test002.javaとsample/test/Test003.java
public static final int DEFAULT_VALUE = 0x20;
public int[] a = new int[128];
public int x, y;
public void init(){
for(int i = 0; i < a.length; i++){
a[i] = 0;
}
}
public class Test003 {
private final Test002 t = new Test002();
14
public void set(int i, int v){
a[i] = v;
}
…
}
public void test(){
t.init();
t.set(0, 100); // 0 <- 100
…
}
…
}
18. インスタンス生成の例
乱数生成のハードウェアをJavaから使う
// xorshift RNG, period 2^128-1
// cf. http://www.jstatsoft.org/v08/i14/paper
module xor128
((
input wire clk, input wire reset, output reg [63:0] q
);
reg[31:0] x = 123456789, y = 362436069, z = 521288629, w = 88675123;
reg[31:0] t;
always @(posedge clk) begin
q[63:32] <= 32'h0;
if(reset == 1'b1) begin
x <= 123456789; y <= 362436069;
z <= 521288629; w <= 88675123;
18
z <= 521288629; w <= 88675123;
end else begin
t = x ^ (x << 11); x = y; y = z; z = w;
w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
q[31:0] <= w;
end
end
endmodule
23. クラスの継承も可
public class Test010{
public void test(){
int a = 20;
int b = 30;
int c = 0;
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
int c = 0;
c = a & b;
c = a | b;
c = a ^ b;
}
}
public class Test012 extends Test010{
public void run(){
test();
use IEEE.numeric_std.all;
entity Test012 is
port (
clk : in std_logic;
reset : in std_logic;
run_busy : out std_logic;
run_req : in std_logic;
test_busy : out std_logic;
test_req : in std_logic
23
test();
}
}
);
end Test012;
architecture RTL of Test012 is
…
24. クラスの継承も可
public abstract class Test013_A{
abstract public int hoge();
}
public class Test013_B extends Test013_A{public class Test013_B extends Test013_A{
public int hoge(){
return 10;
}
}
public class Test013_C extends Test013_A{
public int hoge(){
return 20;
}
public class Test013{
private final Test013_A obj0 = new Test013_B();
24
}
}
private final Test013_A obj0 = new Test013_B();
private final Test013_A obj1 = new Test013_C();
public void test(){
int a = obj0.hoge();
int b = obj1.hoge();
}
}
45. RTLでの設計の場合
…
type stateType is (NONE, CHARGE_5, CHARGE_10, CHARGE_15, OK)
signal state, next_state: stateType := NONE;
…
case (state) is
when NONE =>
if nickel then
next_state <= CHARGE_5;
elsif dime then
next_state <= CHARGE_10;
else
next_state <= NONE;
end if;
when CHARGE_5 =>
if nickel then
next_state <= CHARGE_10;
45
next_state <= CHARGE_10;
elsif dime then
next_state <= CHARGE_15;
else
next_state <= NONE;
end if;
…
46. ベンディングマシンの例
class VendingMachine(n:String,c:String,r:String) extends Module(n,c,r){
val nickel = inP("nickel")
val dime = inP("dime")
val rdy = outP("rdy")
val seq = sequencer("main")val seq = sequencer("main")
val s5,s10,s15,s_ok = seq.add()
rdy <= (seq.idle, LOW)
rdy <= (s_ok, HIGH)
seq.idle -> (nickel, s5)
seq.idle -> (dime, s10)
s5 -> (nickel,s10)
s5 -> (dime, s15)
46
s10 -> (nickel, s15)
s10 -> (dime, s_ok)
s15 -> (nickel, s_ok)
s15 -> (dime, s_ok)
s_ok -> seq.idle
}
val m = new VendingMachine("machine", "clk", "reset")
m.genVHDL()
m.visuallize_statemachine()
54. 自分なりの実装,例
継承とオーバーライドを使って,自然に表現可能
val x0 = inP("x0")
val y0 = inP("y0")val y0 = inP("y0")
val z0 = outP("z0")
val z1 = outP("z1")
val z2 = outP("z2")
z0 := x0 and y0
val x1c = new CARRY4Sig(x0)
val y1c = new CARRY4Sig(y0)
54
val y1c = new CARRY4Sig(y0)
z1 := x1c and y1c
z2 := z0 or z1