LoginSignup
0
0

高位合成言語アセンブラを作る。 その29

Posted at

概要

高位合成言語アセンブラを作る。
論理式から、真理値表を作る。

投入する論理式。

( ( ( a & b ) & ( ~c ) ) | ( ( ~a ) & ( b & c ) ) )

生成するコード。

make 10
wire 3 8
out 9
in 0 2
and 1 2 3
not 0 4
and 4 3 5
not 2 6
and 0 1 7
and 7 6 8
or 8 5 9

サンプルコード


var src = document.getElementById("src");
var out = document.getElementById("out");

function run() {
  var w = "";
  var in0 = 0;
  var in1 = 2;
  var z = 3;
  var v = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
  var str = src.value;
  var codes = str.split("\n");  
  var len = codes.length;
  var i;
  w += "in " + in0 + " " + in1 + "\n";
  for (i = 0; i < len; i++)
  {
    while (codes[i].length > 1) 
    {
      var o = codes[i].lastIndexOf("(");
      var p = codes[i].indexOf(")", o);
      var x = 0;
      var y = "";
      code = codes[i].substring(o + 2, p - 1);
      var co = code.split(" ");
      if (co.length == 1)
      {
        switch (co[0])
        {
        case "":
        break;
        case "~a":
          w += "not 0 " + z + "\n";
          z++;
        break;
        case "~c":
          w += "not 2 " + z + "\n";
          z++;
        break;
        default:
          alert(co[0]);
        break;
        }
      }
      if (co.length == 3)
      {
        switch (co[0])
        {
        case "":
        break;
        case "a":
          x = 0;
        break;
        case "b":
          x = 1;
        break;
        case "c":
          x = 2;
        break;
        case "d":
          x = 3;
        break;
        case "e":
          x = 4;
        break;
        case "f":
          x = 5;
        break;
        case "g":
          x = 6;
        break;
        case "h":
          x = 7;
        break;
        case "i":
          x = 8;
        break;
        case "j":
          x = 9;
        break;
        default:
          alert(co[0]);
        break;
        }
        switch (co[1])
        {
        case "":
        break;
        case "&":
          y = "and ";
        break;
        case "|":
          y = "or ";
        break;
        default:
          alert(co[1]);
        break;
        }
        switch (co[2])
        {
        case "":
        break;
        case "a":
          w += y + x + " 0 " + z + "\n";
          z++;
        break;
        case "b":
          w += y + x + " 1 " + z + "\n";
          z++;
        break;
        case "c":
          w += y + x + " 2 " + z + "\n";
          z++;
        break;
        case "d":
          w += y + x + " 3 " + z + "\n";
          z++;
        break;
        case "e":
          w += y + x + " 4 " + z + "\n";
          z++;
        break;
        case "f":
          w += y + x + " 5 " + z + "\n";
          z++;
        break;
        case "g":
          w += y + x + " 6 " + z + "\n";
          z++;
        break;
        case "h":
          w += y + x + " 7 " + z + "\n";
          z++;
        break;
        case "i":
          w += y + x + " 8 " + z + "\n";
          z++;
        break;
        case "j":
          w += y + x + " 9 " + z + "\n";
          z++;
        break;
        default:
          alert(co[2]);
        break;
        }
      }
      codes[i] = codes[i].substring(0, o) + v[z - 1] + codes[i].substring(p + 1);
    }
  }
  out.value = "make " + z + "\n";
  out.value += "wire 3 " + (z - 2) + "\n";
  out.value += "out " + (z - 1) + "\n";  
  out.value += w;
}




成果物

以上。

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