jsdoでchip8　その4

#概要

jsdoでchip8やってみた。
アセンブラ作ってみた。

#写真

![image](https://qiita-image-store.s3.amazonaws.com/0/18104/e0b930ae-99b4-126e-bf24-479703ee81a1.png)

#サンプルコード

```
function _arrayBufferToBase64(buffer) {
    var binary = '';
    var bytes = new Uint8Array(buffer);
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) 
    {
        binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
}
function tob(url) {
    var bin = atob(url);
    var buffer = new Uint8Array(bin.length);
    for (var i = 0; i < bin.length; i++)
    {
  	    buffer[i] = bin.charCodeAt(i);
    }
    var blob = new Blob([buffer.buffer], {
        type : 'application/octet-binary'
    });
    var blob_url = window.URL.createObjectURL(blob);
    return blob_url;
}
function run() {
	var c1, 
        c2, 
        c3, 
        a, 
        i, 
        j;
	var pc = 0;
    var la = new Array(10);
	var r = new Array(100);
    var src = document.getElementById('src0').value;
	var m = src.split("\n");
	for (j = 0; j < m.length; j++)
	{
        var cm = m[j].split("  ");
		if (cm[0] != "")
        {
            la[cm[0]] = pc + 2;
        }
		switch (cm[1])
		{
		case "disp":
            var x = 0;
            switch (cm[2])
		    {
		    case "v0":
                x = 0;
            break;
            case "v1":
                x = 1;
            break;        
            case "v2":
                x = 2;
            break;        
            case "v3":
                x = 3;
            break;        
            case "v4":
                x = 4;
            break;        
            case "v5":
                x = 5;
            break;
            case "v6":
                y = 6;
            break;            
            case "v7":
                x = 7;
            break;        
            case "v8":
                x = 8;
            break;        
            case "v9":
                x = 9; 
            break;        
            case "v10":
                x = 10;
            break;        
            case "v11":
                x = 11;
            break;        
            case "v12":
                x = 12;
            break;        
            case "v13":
                x = 13;
            break;        
            case "v14":
                x = 14;
            break;        
            case "v15":
                x = 15;
            break;        
            }
            var y = 0;
            switch (cm[3])
		    {
		    case "v0":
                y = 0;
            break;        
            case "v1":
                y = 1;
            break;        
            case "v2":
                y = 2;
            break;        
            case "v3":
                y = 3;
            break;        
            case "v4":
                y = 4;
            break;        
            case "v5":
                y = 5;
            break;
            case "v6":
                y = 6;
            break;            
            case "v7":
                y = 7;
            break;        
            case "v8":
                y = 8;
            break;        
            case "v9":
                y = 9;
            break;        
            case "v10":
                y = 10;
            break;        
            case "v11":
                y = 11;
            break;        
            case "v12":
                y = 12;
            break;        
            case "v13":
                y = 13;
            break;        
            case "v14":
                y = 14;
            break;        
            case "v15":
                y = 15;
            break;        
            }    
            r[pc] = 0xD0 + x;
			pc++;
			r[pc] = y * 16 + Number(cm[4]);
            pc++;
		break;
		case "add":
            if (cm[2] == "I")
            {
                var x = 0;
                switch (cm[3])
		        {
		        case "v0":
                    x = 0;
                break;        
                case "v1":
                    x = 1;
                break;        
                case "v2":
                    x = 2;
                break;        
                case "v3":
                    x = 3;
                break;        
                case "v4":
                    x = 4;
                break;        
                case "v5":
                    x = 5;
                break;  
                case "v6":
                    y = 6;
                break;            
                case "v7":
                    x = 7;
                break;        
                case "v8":
                    x = 8;
                break;        
                case "v9":
                    x = 9;
                break;        
                case "v10":
                    x = 10;
                break;        
                case "v11":
                    x = 11;
                break;        
                case "v12":
                    x = 12;
                break;        
                case "v13":
                    x = 13;
                break;        
                case "v14":
                    x = 14;
                break;        
                case "v15":
                    x = 15;
                break;        
                }
                //alert(x);
                r[pc] = 0xF0 + x;
		    	pc++;
			    r[pc] = 0x1E;
			    pc++;
            }
            else if (cm[3].indexOf("v") > -1)
            {
                var x = 0;
                switch (cm[2])
		        {
		        case "v0":
                    x = 0;
                break;        
                case "v1":
                    x = 1;
                break;        
                case "v2":
                    x = 2;
                break;        
                case "v3":
                    x = 3;
                break;        
                case "v4":
                    x = 4;
                break;        
                case "v5":
                    x = 5;
                break;
                case "v6":
                    y = 6;
                break;            
                case "v7":
                    x = 7;
                break;        
                case "v8":
                    x = 8;
                break;        
                case "v9":
                    x = 9;
                break;        
                case "v10":
                    x = 10;
                break;        
                case "v11":
                    x = 11;
                break;        
                case "v12":
                    x = 12;
                break;        
                case "v13":
                    x = 13;
                break;        
                case "v14":
                    x = 14;
                break;        
                case "v15":
                    x = 15;
                break;        
                }
                var y = 0;
                switch (cm[3])
		        {
		        case "v0":
                    y = 0;
                break;        
                case "v1":
                    y = 1;
                break;        
                case "v2":
                    y = 2; 
                break;        
                case "v3":
                    y = 3; 
                break;        
                case "v4":
                    y = 4; 
                break;        
                case "v5":
                    y = 5; 
                break;
                case "v6":
                    y = 6;
                break;            
                case "v7":
                    y = 7; 
                break;        
                case "v8":
                    y = 8; 
                break;        
                case "v9":
                    y = 9; 
                break;        
                case "v10":
                    y = 10; 
                break;        
                case "v11":
                    y = 11; 
                break;        
                case "v12":
                    y = 12; 
                break;        
                case "v13":
                    y = 13; 
                break;        
                case "v14":
                    y = 14; 
                break;        
                case "v15":
                    y = 15; 
                break;       
                }            
	    		r[pc] = 0x80 + x;
    			pc++;
	    		r[pc] = y * 16 + 4;
		    	pc++;
            }
            else
            {
                var x = 0;
                switch (cm[2])
		        {
		        case "v0":
                    x = 0;
                break;        
                case "v1":
                    x = 1;
                break;        
                case "v2":
                    x = 2; 
                break;        
                case "v3":
                    x = 3; 
                break;        
                case "v4":
                    x = 4; 
                break;        
                case "v5":
                    x = 5; 
                break;
                case "v6":
                    y = 6;
                break;            
                case "v7":
                    x = 7;
                break;        
                case "v8":
                    x = 8; 
                break;        
                case "v9":
                    x = 9; 
                break;        
                case "v10":
                    x = 10; 
                break;        
                case "v11":
                    x = 11; 
                break;        
                case "v12":
                    x = 12; 
                break;        
                case "v13":
                    x = 13; 
                break;        
                case "v14":
                    x = 14; 
                break;        
                case "v15":
                    x = 15; 
                break;        
                }
                r[pc] = 0x70 + x;
    			pc++;
	    		r[pc] = cm[3];
		    	pc++;
            }
		break;
		case "load":
			if (cm[2] == "I")
            {
                r[pc] = 0xA0 + Math.floor(cm[3] / 256);
		    	pc++;
                r[pc] = cm[3] % 256;
			    pc++;
            }
            else
            {
                var x = 0;
                switch (cm[2])
		        {
		        case "v0":
                    x = 0;
                break;        
                case "v1":
                    x = 1;
                break;        
                case "v2":
                    x = 2; 
                break;        
                case "v3":
                    x = 3; 
                break;        
                case "v4":
                    x = 4;
                break;        
                case "v5":
                    x = 5; 
                break;
                case "v6":
                    y = 6;
                break;           
                case "v7":
                    x = 7; 
                break;        
                case "v8":
                    x = 8; 
                break;        
                case "v9":
                    x = 9; 
                break;        
                case "v10":
                    x = 10; 
                break;        
                case "v11":
                    x = 11; 
                break;       
                case "v12":
                    x = 12; 
                break;        
                case "v13":
                    x = 13; 
                break;        
                case "v14":
                    x = 14; 
                break;        
                case "v15":
                    x = 15; 
                break;        
                }
               	r[pc] = 0x60 + x;
    			pc++;
	    		r[pc] = Number(cm[3]);
		    	pc++;
            }
		break;
		}
	}
    for (var a in la) 
	{

        for (i = 0; i < pc; i++)
		{
			if (r[i] == a)
			{

                c3 = la[a] - i - 4;
				r[i] = c3;
			}
		}
	}
    var b = _arrayBufferToBase64(r);
    var url1 = tob(b);
    var a = document.createElement('a');    
    a.download = "test.rom";
    a.href = url1;
    a.textContent = "Download rom";
    document.getElementById('out').appendChild(a);
}


```

#成果物

http://jsdo.it/ohisama1/YKu5

以上。