windowsでiverilog その164

# 概要　
windowsでiverilogやってみた。
cpu見つけたので、やってみた。
アセンブラ書いてみた。

# 参考にしたページ

https://qiita.com/yoshiki9636/items/62557960699dd92d3008

# サンプルコード

```

var src = document.getElementById('src');
var out = document.getElementById('out');
var la = [];

function hex(str) {
	var i = parseInt(str);
	//alert(i);
	return ('00' + i.toString(16)).slice(-2) + ";\n";
}
function run() {
  var str = src.value; 
  var codes = str.split("\n");  
  var len = codes.length - 1;
	for (var i = 0; i < len; i++)
	{
		var cm = codes[i].split("  ");
		if (cm[0] != "")
		{
			la[cm[0]] = i * 2;
			//alert(cm[0]);
		}
	}
	for (var i = 0; i < len; i++)
	{
		out.value += "// " + codes[i] + "\n";
		var cm = codes[i].split("  ");
		switch (cm[1])
		{
		case "LOAD":
			switch (cm[2])
			{
			case "A":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h01;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h08;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			case "D":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h10;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			}
		break;
		case "ADD":
			switch (cm[2])
			{
			case "A":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h02;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h0c;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			}
		break;
		case "SUB":
			switch (cm[2])
			{
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h0d;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			case "D":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h11;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			}
		break;
		case "XOR":
			switch (cm[2])
			{
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h09;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			case "C":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h0b;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(cm[3]);
			break;
			}
		break;
		case "OUT":
			switch (cm[2])
			{
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h05;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h00;\n";
			break;
			case "C":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h0a;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h00;\n";
			break;
			}
		break;
		case "JMP":
		//alert(la[cm[3]]);
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h04;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(la[cm[2]]);
		break;
		case "JNZ":
			switch (cm[2])
			{
			case "A":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h07;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(la[cm[3]]);
			break;
			case "B":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h0e;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(la[cm[3]]);
			break;
			case "D":
				out.value += "assign rom_program[" + i * 2 + "] <= 8'h12;\n";
				out.value += "assign rom_program[" + (i * 2 + 1) + "] <= 8'h" + hex(la[cm[3]]);
			break;
			}
		break;
		}
	}
}




```

# 投入したソース

```
start  LOAD  D  255
to2  LOAD  B  255
to4  LOAD  A  255
to6  ADD  A  255
  JNZ  A  to6
  SUB  B  1
  JNZ  B  to4
  SUB  D  1
  JNZ  D  to2
  OUT  C
  XOR  C  1
  LOAD  A  255
  JMP  start

```

# 生成したマシン語

```
// start  LOAD  D  255
assign rom_program[0] <= 8'h10;
assign rom_program[1] <= 8'hff;
// to2  LOAD  B  255
assign rom_program[2] <= 8'h08;
assign rom_program[3] <= 8'hff;
// to4  LOAD  A  255
assign rom_program[4] <= 8'h01;
assign rom_program[5] <= 8'hff;
// to6  ADD  A  255
assign rom_program[6] <= 8'h02;
assign rom_program[7] <= 8'hff;
//   JNZ  A  to6
assign rom_program[8] <= 8'h07;
assign rom_program[9] <= 8'h06;
//   SUB  B  1
assign rom_program[10] <= 8'h0d;
assign rom_program[11] <= 8'h01;
//   JNZ  B  to4
assign rom_program[12] <= 8'h0e;
assign rom_program[13] <= 8'h04;
//   SUB  D  1
assign rom_program[14] <= 8'h11;
assign rom_program[15] <= 8'h01;
//   JNZ  D  to2
assign rom_program[16] <= 8'h12;
assign rom_program[17] <= 8'h02;
//   OUT  C
assign rom_program[18] <= 8'h0a;
assign rom_program[19] <= 8'h00;
//   XOR  C  1
assign rom_program[20] <= 8'h0b;
assign rom_program[21] <= 8'h01;
//   LOAD  A  255
assign rom_program[22] <= 8'h01;
assign rom_program[23] <= 8'hff;
//   JMP  start
assign rom_program[24] <= 8'h04;
assign rom_program[25] <= 8'h00;

```

# 成果物

https://embed.plnkr.co/plunk/HBPN0r5DAju9YOaK

以上。