0
0

ビジュアルプログラミングで高位合成 その9

Posted at

概要

ビジュアルプログラミングで組み合わせ回路を組み立てて、シュミレーションして、verilogを生成します。
高位合成です。
実は、今の実装では、全加算器が高位合成できません。
全加算器の高位合成を実装します。
実装編

成果物

生成したverilog

module x(input B, input A, input Cin, output Sum, output Cout);
	wire w0;
	wire w1;
	wire w2;
	wire w3;
	assign Sum = (Cin ^ w0);
	assign w0 = (A ^ B);
	assign w1 = (A ^ B);
	assign w2 = (A & B);
	assign Cout = (w3 | w2);
	assign w3 = (Cin & w1);
endmodule

サンプルコード


function make(s) {
	var i;
	var j;
	var k;
	var l;
	var res = "";
	var ins = [];
	var outs = [];
	var divs = [];
	var wire = [];
	var n = 0;
	var ass = [];
	var json = JSON.parse(s);
 	var koko = document.getElementById("koko");
	for (i = 0; i < json.devices.length; i++) 
	{
    if (json.devices[i].type == "In")
    {
			ins.push({
				id: json.devices[i].id,
				label: json.devices[i].label
			});
    }
	}
	for (i = 0; i < json.devices.length; i++) 
	{
    if (json.devices[i].type == "Out")
    {
			outs.push({
				id: json.devices[i].id,
				label: json.devices[i].label
			});
    }
	}
	for (i = 0; i < json.devices.length; i++) 
	{
    if (json.devices[i].type == "XOR" || json.devices[i].type == "NAND" || json.devices[i].type == "AND" || json.devices[i].type == "OR" || json.devices[i].type == "NOR" || json.devices[i].type == "NXOR")
    {
			divs.push({
				id: json.devices[i].id,
				type: json.devices[i].type,
				label: json.devices[i].label
			});
    }
	}
	for (k = 0; k < divs.length; k++) 
	{
		for (i = 0; i < json.connectors.length; i++) 
		{
	 	  if (json.connectors[i].to == divs[k].id + ".out0")
  	  {
				for (j = 0; j < divs.length; j++) 
				{
		    	if (divs[j].id + ".in0" == json.connectors[i].from || divs[j].id + ".in1" == json.connectors[i].from)
					{
						wire.push({
							in: json.connectors[i].from,
							out: json.connectors[i].to,
							id: "w" + n
						});
						n++;
					}					
  	  	}
			}
		}
	}
	for (k = 0; k < divs.length; k++)
	{
		for (i = 0; i < json.connectors.length; i++)
		{
			if (json.connectors[i].to == divs[k].id + ".out0")
			{
				var v = 0;
				for (j = 0; j < wire.length; j++)
				{
					if (json.connectors[i].to == wire[j].out && json.connectors[i].from == wire[j].in)
					{
						ass.push({
							in: json.connectors[i].from,
							out: json.connectors[i].to,
							in0: "",
							in1: "",
							out0: "",
							type: divs[k].type,
							label: divs[k].label,
							id1: json.connectors[i].to.replace(".out0", ""),
							id: wire[j].id
						});
						v = 1;
					}
				}
				if (v == 0)
				{
					for (j = 0; j < json.devices.length; j++)
					{
						if (json.devices[j].id + ".in0" == json.connectors[i].from)
						{
							ass.push({
								in: json.connectors[i].from,
								out: json.connectors[i].to,
								in0: "",
								in1: "",
								out0: "",
								type: divs[k].type,
								label: divs[k].label,
								id1: json.connectors[i].to.replace(".out0", ""),
								id: json.devices[j].label
							});
						}
					}
				}
			}
		}
	}
	res += "module x(";
	if (ins.length == 3)
	{
		res += "input " + ins[0].label + ", ";
		res += "input " + ins[1].label + ", ";
		res += "input " + ins[2].label + ", ";
	}
	if (ins.length == 2)
	{
		res += "input " + ins[0].label + ", ";
		res += "input " + ins[1].label + ", ";
	}
	if (outs.length == 1)
	{
		res += "output " + outs[0].label + ");\n";
	}
	if (outs.length == 2)
	{
		res += "output " + outs[0].label + ", ";
		res += "output " + outs[1].label + ");\n";
	}
	for (k = 0; k < wire.length; k++) 
	{
		res += "\twire " + wire[k].id + ";\n";
	}
	for (k = 0; k < ass.length; k++)
	{
		res += "\tassign " + ass[k].id + " = (";
		for (i = 0; i < json.connectors.length; i++)
		{		
			if (json.connectors[i].from == ass[k].id1 + ".in0")
			{
				var v = 0;
				for (j = 0; j < wire.length; j++)
				{
					if (json.connectors[i].to == wire[j].out && json.connectors[i].from == wire[j].in)
					{
						res += wire[j].id;
						v = 1;
					}
				}
				if (v == 0)
				{
					for (j = 0; j < json.devices.length; j++)
					{
						if (json.devices[j].id + ".out0" == json.connectors[i].to)
						{
							res += json.devices[j].label;
						}
					}
				}
			}
		}	
		if (ass[k].type == "XOR")
		{
			res += " ^ ";
		}
		if (ass[k].type == "AND")
		{
			res += " & ";
		}
		if (ass[k].type == "OR")
		{
			res += " | ";
		}
		if (ass[k].type == "NAND")
		{
			res += " ~& ";
		}
		if (ass[k].type == "NOR")
		{
			res += " ~| ";
		}
		if (ass[k].type == "XNOR")
		{
			res += " ~^ ";
		}
		for (i = 0; i < json.connectors.length; i++)
		{		
			if (json.connectors[i].from == ass[k].id1 + ".in1")
			{
				var v = 0;
				for (j = 0; j < wire.length; j++)
				{
					if (json.connectors[i].to == wire[j].out && json.connectors[i].from == wire[j].in)
					{
						res += wire[j].id;
						v = 1;
					}
				}
				if (v == 0)
				{
					for (j = 0; j < json.devices.length; j++)
					{
						if (json.devices[j].id + ".out0" == json.connectors[i].to)
						{
							res += json.devices[j].label;
						}
					}
				}
			}
		}					
		res += ");\n";
	}
	res += "endmodule";
	koko.value = res;
}

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0