概要
高位合成言語アセンブラを作る。
練習問題、やってみる。
練習問題
3 の正の倍数検出回路を作れ。
高位合成する。
投入したソースコード
make 10
in 0 2
wire 3 8
not 2 3
and 0 1 4
and 1 2 5
not 0 6
and 3 4 7
and 5 6 8
or 7 8 9
out 9 9
サンプルコード
var src = document.getElementById("src");
var out = document.getElementById("out");
function n2s(n) {
var s = String.fromCharCode(n + 97);
return s;
}
function run() {
var in0 = "";
var in1 = "";
var in2 = "";
var out0 = "";
var make0 = " x u(";
var make1 = " initial\n begin\n $display(\"";
var make2 = " $monitor(\"";
var make3 = "";
var u = "";
var str = src.value;
var codes = str.split("\n");
var len = codes.length;
var i, j;
for (i = 0; i < len; i++)
{
var code = codes[i].split(" ");
switch (code[0])
{
case "":
break;
case "wire":
break;
case "make":
break;
case "in":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
switch (b)
{
case 0:
in2 =
" a = 0; #10;\n" +
" a = 1; #10;\n";
break;
case 1:
in2 =
" a = 0; b = 0; #10;\n" +
" a = 0; b = 1; #10;\n" +
" a = 1; b = 0; #10;\n" +
" a = 1; b = 1; #10;\n";
break;
case 2:
in2 =
" a = 0; b = 0; c = 0; #10;\n" +
" a = 0; b = 0; c = 1; #10;\n" +
" a = 0; b = 1; c = 0; #10;\n" +
" a = 0; b = 1; c = 1; #10;\n" +
" a = 1; b = 0; c = 0; #10;\n" +
" a = 1; b = 0; c = 1; #10;\n" +
" a = 1; b = 1; c = 0; #10;\n" +
" a = 1; b = 1; c = 1; #10;\n";
break;
case 3:
in2 =
" a = 0; b = 0; c = 0; d = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; #10;\n";
break;
case 4:
in2 =
" a = 0; b = 0; c = 0; d = 0; e = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 0; e = 1; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 1; #10;\n";
break;
case 5:
in2 =
" a = 0; b = 0; c = 0; d = 0; e = 0; f = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 0; e = 0; f = 1; #10;\n" +
" a = 0; b = 0; c = 0; d = 0; e = 1; f = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 0; e = 1; f = 1; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 0; f = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 0; f = 1; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 1; f = 0; #10;\n" +
" a = 0; b = 0; c = 0; d = 1; e = 1; f = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 0; f = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 0; f = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 1; f = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 0; e = 1; f = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 0; f = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 0; f = 1; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 1; f = 0; #10;\n" +
" a = 0; b = 0; c = 1; d = 1; e = 1; f = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 0; f = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 0; f = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 1; f = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 0; e = 1; f = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 0; f = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 0; f = 1; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 1; f = 0; #10;\n" +
" a = 0; b = 1; c = 0; d = 1; e = 1; f = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 0; f = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 0; f = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 1; f = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 0; e = 1; f = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 0; f = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 0; f = 1; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 1; f = 0; #10;\n" +
" a = 0; b = 1; c = 1; d = 1; e = 1; f = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 0; f = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 0; f = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 1; f = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 0; e = 1; f = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 0; f = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 0; f = 1; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 1; f = 0; #10;\n" +
" a = 1; b = 0; c = 0; d = 1; e = 1; f = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 0; f = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 0; f = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 1; f = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 0; e = 1; f = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 0; f = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 0; f = 1; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 1; f = 0; #10;\n" +
" a = 1; b = 0; c = 1; d = 1; e = 1; f = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 0; f = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 0; f = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 1; f = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 0; e = 1; f = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 0; f = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 0; f = 1; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 1; f = 0; #10;\n" +
" a = 1; b = 1; c = 0; d = 1; e = 1; f = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 0; f = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 0; f = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 1; f = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 0; e = 1; f = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 0; f = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 0; f = 1; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 1; f = 0; #10;\n" +
" a = 1; b = 1; c = 1; d = 1; e = 1; f = 1; #10;\n";
break;
}
b++;
for (j = 0; j < b; j++)
{
make0 += "." + n2s(j) + "(" + n2s(j) + "), ";
make1 += n2s(j) + " ";
make2 += "%b ";
make3 += n2s(j) + ", ";
}
for (j = a; j < b; j++)
{
in0 += "input " + n2s(j) + ", ";
in1 += n2s(j) + ", ";
}
in1 = in1.substr(0, in1.length - 2);
break;
case "out":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
b++;
for (j = a; j < b; j++)
{
out0 += "output " + n2s(j) + ", ";
make0 += "." + n2s(j) + "(" + n2s(j) + "), ";
make1 += n2s(j) + " ";
make2 += "%b ";
make3 += n2s(j) + ", ";
}
out0 = out0.substr(0, out0.length - 2);
make0 = make0.substr(0, make0.length - 2);
make3 = make3.substr(0, make3.length - 2);
make0 += ");\n";
make1 += "\");\n";
make2 += "\", ";
make3 += ");\n";
break;
case "nand":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
var c = parseInt(code[3]);
u += " assign " + n2s(c) + " = ~(" + n2s(a) + " & " + n2s(b) + ");\n";
break;
case "xor":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
var c = parseInt(code[3]);
u += " assign " + n2s(c) + " = " + n2s(a) + " ^ " + n2s(b) + ";\n";
break;
case "and":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
var c = parseInt(code[3]);
u += " assign " + n2s(c) + " = " + n2s(a) + " & " + n2s(b) + ";\n";
break;
case "or":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
var c = parseInt(code[3]);
u += " assign " + n2s(c) + " = " + n2s(a) + " | " + n2s(b) + ";\n";
break;
case "nor":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
var c = parseInt(code[3]);
u += " assign " + n2s(c) + " = ~(" + n2s(a) + " | " + n2s(b) + ");\n";
break;
case "not":
var a = parseInt(code[1]);
var b = parseInt(code[2]);
u += " assign " + n2s(b) + " = ~(" + n2s(a) + ");\n";
break;
default:
alert(code[0]);
break;
}
}
out.value = "module x(" + in0 + out0 + ");\n" +
u +
"endmodule\n\n" +
"module testbench;\n" +
" reg " + in1 + ";\n" +
make0 + make1 + make2 + make3 +
in2 +
" $finish;\n" +
" end\n" +
"endmodule";
}
実行結果
module x(input a, input b, input c, output j);
assign d = ~(c);
assign e = a & b;
assign f = b & c;
assign g = ~(a);
assign h = d & e;
assign i = f & g;
assign j = h | i;
endmodule
module testbench;
reg a, b, c;
x u(.a(a), .b(b), .c(c), .j(j));
initial
begin
$display("a b c j ");
$monitor("%b %b %b %b ", a, b, c, j);
a = 0; b = 0; c = 0; #10;
a = 0; b = 0; c = 1; #10;
a = 0; b = 1; c = 0; #10;
a = 0; b = 1; c = 1; #10;
a = 1; b = 0; c = 0; #10;
a = 1; b = 0; c = 1; #10;
a = 1; b = 1; c = 0; #10;
a = 1; b = 1; c = 1; #10;
$finish;
end
endmodule
成果物
以上。