Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: yrinfish on May 24, 2011, 08:43:52 am

Title: [JS] A small 4-bit interpreter/emulator
Post by: yrinfish on May 24, 2011, 08:43:52 am
I was bored, and I have a obsession with... Nevermind, here it is.

Made with my iPod Touch (no copy-paste used, everything is hand-codedthumb-coded)
Why js?
Because I hate Apple Waaah. Er, no, because I can't program in any other language with it thanks to Apple. (ok, it's jailbroken, but php is just ... not right for this and bash... There are still windows users here...

Why 4-bit?
To keep it simple, stupid.

And...?
42.





script.js
Code: [Select]
var RAM = [4, 3, 5, 9, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0];
var ip = 0;
var a = 0;
var b = 0;
var c = 0; // for easy xchg
var z = false;



function setFlags(num) {
    if(num == 0) {
        z = true;
    }else{
        z = false;
    }
}

function execOpCode(opCode, arg) {
    var opCodeLength=1;

    switch(opCode){
    case 0:
        break;
    case 1:
        opCodeLength = 0;
        ip = arg;
        break;
    case 2:
        opCodeLength = 2;
        if(!z){
            ip = arg;
            opCodeLength = 0;
        }
        break;
    case 3:
        opCodeLength = 2;
        if(z){
            ip = arg;
            opCodeLength = 0;
        }
        break;
    case 4:
        opCodeLength = 2;
        a = arg;
        break;
    case 5:
        opCodeLength = 2;
        b = arg;
        break;
    case 6:
        opCodeLength = 2;
        RAM[arg] = a;
        break;
    case 7:
        opCodeLength = 2;
        a = RAM[arg];
        break;
    case 8:
        a = abs(a+b);
        setFlags(a);
        break;
    case 9:
        a = abs(a|b);
        setFlags(a);
        break;
    case 10:
        a = abs(a^b);
        setFlags(a);
        break;
    case 11:
        a = abs(a&b);
        setFlags(a);
        break;
    case 12:
        a = abs(~a);
        setFlags(a);
        break;
    case 13:
        a = abs(-a);
        setFlags(a);
        break;
    case 14:
        c = b;
        b = a;
        a = c;
        break;
    case 15:
        a = abs(a-b);
        setFlags(a);
        break;
    default:
        break;
    }
    return opCodeLength;
}

function log(txt) {
    document.querySelector('div#log').innerHTML += txt;
}

function hex(num) {
    var string = num;
    var abcdef = ['a', 'b', 'c', 'd', 'e', 'f'];

    if(num>15) {
        num = abs(num);
    }

    if(num>9) {
        string = abcdef[num-10];
    }
    return string;
}

function abs(num) {
    return num < 0 ? (16-Math.abs(num%16))%16 : num%16;
}

function sanitize() {
    var j = 0;

    for(j=0;j<16;j++) {
        RAM[j] = abs(RAM[j]);
    }
    a = abs(a);
    b = abs(b);
}

function dumpRAM() {
    var k = 0;

    log('[');
    for(k=0;k<16;k++) {
        log(hex(RAM[k]));
    }
    log(']');
}

function parseRAMInputs() {
    var i = 0;
    for(i=0;i<16;i++) {
        RAM[i] = document.getElementById('RAM' + i).value;
/*        document.getElementById('RAM' + i).setAttribute('onclick', 'this.value = ''"');*/
    }
}

function start() {
    var i = 0;
    var counter=0;
    var maxCycles = document.getElementById('cycles').value;

    a = 0;
    b = 0;
    ip = 0;

    parseRAMInputs();
    sanitize();
    log('Starting…<br /> RAM: ');
    dumpRAM();
    log('<br />Cycles to simulate: '+maxCycles+' <br /><br />');
    i=0;
    while(ip<16 && counter<maxCycles) {
        log(hex(ip) + ':' + hex(RAM[ip]));
        ip = execOpCode(RAM[ip], RAM[abs(ip+1)]) + ip;
        log('   a=' + hex(a) + '   b=' + hex(b) + '<br />');
        counter++;
    }
    log('<br />');
    if(counter==maxCycles) {
        log('Stopped: max cycles reached.<br />');
    }
    log('RAM:       ');
    dumpRAM();
    log('<br />register a=                '+hex(a));
    log('<br />register b=                '+hex(b));
    log('<br />instruction pointer ip=    '+hex(ip));
    log('<br />zero flag z=            '+z+'<br />');
    return;
}


.html
Code: [Select]
<!DOCTYPE html>
<html>
    <head>
        <title>interpreter</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <script src="script.js"></script>
    </head>
    <body onload="42">
        <form id="settings">
            <label for="cycles">Cycles</label>
            <input id="cycles" type="textbox" value="32" />
            
            <div id="RAM">
                <input id="RAM0" type="textbox" value="0" size="1" />
                <input id="RAM1" type="textbox" value="0" size="1" />
                <input id="RAM2" type="textbox" value="0" size="1" />
                <input id="RAM3" type="textbox" value="0" size="1" />

                <input id="RAM4" type="textbox" value="0" size="1" />
                <input id="RAM5" type="textbox" value="0" size="1" />
                <input id="RAM6" type="textbox" value="0" size="1" />
                <input id="RAM7" type="textbox" value="0" size="1" />

                <br />

                <input id="RAM8" type="textbox" value="0" size="1" />
                <input id="RAM9" type="textbox" value="0" size="1" />
                <input id="RAM10" type="textbox" value="0" size="1" />
                <input id="RAM11" type="textbox" value="0" size="1" />

                <input id="RAM12" type="textbox" value="0" size="1" />
                <input id="RAM13" type="textbox" value="0" size="1" />
                <input id="RAM14" type="textbox" value="0" size="1" />
                <input id="RAM15" type="textbox" value="0" size="1" />
            </div>
            
            <input id="submit" type="button" value="Simulate" onclick="start()" />
        </form>
        <tt><div id="log" style="white-space:pre;"></div></tt>
    </body>
</html>
Title: Re: [JS] A small 4-bit interpreter/emulator
Post by: TIfanx1999 on May 24, 2011, 09:16:05 am
I moved the original. This one is locked pending deletion, as I don't have the authority to delete it. D:
Title: Re: [JS] A small 4-bit interpreter/emulator
Post by: DJ Omnimaga on May 24, 2011, 03:02:47 pm
Er I removed the wrong topic I think, but it seemed to have the same content anyway, so I'll unlock this one.
Title: Re: [JS] A small 4-bit interpreter/emulator
Post by: yrinfish on May 24, 2011, 03:05:42 pm
You did lol, but it does have the same content, so it doesn't matter