0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windowsでiverilog その166

Last updated at Posted at 2025-05-16

概要 

windowsでiverilogやってみた。
cpu見つけたので、勝手にマシン語、作ってみた。
アセンブラが動くインタープリター書いてみた。

参考にしたページ

写真

image.png

投入したソース

main  psh  5       
  psh  8
  psh  2
  cal  sum     
  pop  ar      
  mov  ba  8   
  sta  ba  ar  
  mov  va  ar
  hlt         
sum  mov  ar  0           
loop_start  mov  cr  sp
  nor  cr  cr          
  jmp  cr  loop_end    
loop_body  pop  dr              
  add  ar  dr          
  jmp  1  loop_start   
loop_end  psh  ar              
  ret

実行結果

AR : 15
CR : 1
DR : 5
BA : 8
SP : 0
VA : 15

サンプルコード


var src = document.getElementById('src');
var out = document.getElementById('out');
var stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

function spush() {
  for (var i = 10; i > 0; i--)
		stack[i] = stack[i - 1];
}
function spop() {
  for (var i = 0; i < 9; i++)
		stack[i] = stack[i + 1];
}

function run() {
	var la = [];
	var pc = 0;
	var AR = 0;
	var CR = 0;
	var DR = 0;
 	var BA = 0;
	var SP = 0;
  var RA = 0;
	var ram = [];
  var i = 0;
  var j = 1;
	var str = src.value; 
  var codes = str.split("\n");  
  var len = codes.length - 1;
	for (i = 0; i < len; i++)
	{
		var cm = codes[i].split("  ");
		if (cm[0] != "")
		{
			la[cm[0]] = i;
		}
	}
  while (j > 0)
  {
		//alert(codes[pc]);
    var code = codes[pc].split("  ");
    //alert(code[1]);
    switch (code[1])
    {
    case "psh":
      spush();
      if (code[2] == "ar") 
      {
        stack[0] = AR;
      }
      else
      {  
        stack[0] = parseFloat(code[2]);
      }
      //alert(stack[0]);
      //alert(stack[1]);
      //alert(stack[2]);
      SP++;
      pc++;
    break;
    case "pop":
      //alert(stack[0]);
      if (code[2] == "ar") 
      {
        AR = stack[0];
      }
      else
      {  
        DR = stack[0];
      }
      spop();
      SP--;
      pc++;
    break;
    case "sta":
      ram[BA] = AR;
      pc++;
    break;
    case "nor":
      if (CR == 0)
      {
        CR = 1;
      }
      else
      {
        CR = 0; 
      }
      pc++;
    break; 
    case "mov":
      if (code[2] == "va") 
      {
        VA = AR;
      }
      else if (code[2] == "ba")
      {
        BA = parseFloat(code[3]);
      } 
      else if (code[3] == "sp")
      {
        CR = SP;
      } 
      else 
      {
        AR = parseFloat(code[3]);
      }
      pc++;
    break; 
    case "add":  
      if (code[3] == "dr") 
      {
        AR = AR + DR;
        //alert(AR);
      }
      pc++;
    break;
    case "cal": 
      RA = pc + 1;
      pc = la[code[2]];
    break;
    case "ret":  
      pc = RA;
    break;
    case "jmp": 
      if (code[2] == "1")
      {
        pc = la[code[3]];
      }
      else if (code[2] == "cr")
      {
        if (CR == 1)
        {
          pc = la[code[3]];
        }
        else
        {
          pc++;
        }
      }
      else
      {
        pc++;
      }
    break;
    case "hlt":
      //alert("hlt");
      j = 0;
    break;
    default:
      pc++;
    break;
    }
  }
  out.value += "AR : " + AR + "\n";
  out.value += "CR : " + CR + "\n";
  out.value += "DR : " + DR + "\n";
  out.value += "BA : " + BA + "\n";
  out.value += "SP : " + SP + "\n";
  out.value += "VA : " + VA + "\n";
}




成果物

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?