virtual cpu
Posted: Fri Apr 18, 2003 4:28 pm
having nothing better to do tonight
i knocked out a virtual cpu.
register based
(16 registers,
reg0 is always zero,
reg15 is instusction pointer
reg14 is flags
reg13 is stack pointer)
is a risc design. all register to register except 1 command, which takes a 32bit number in.
there is only 13 commands, some things need to be 'emulated' with other commands (eg: push/pop)
i've already written the virtual machine in C so now all i need to do is write a c-like compiler...
i'm thinking of using it in my stuff as embedded scripting.
i did get a bit lazy, i could have implemeted sub/div/mul all as 'add' commands but i couldnt be arsed I'm strictly following RISC here...
and there is only 2 flags, overflow and equal...
since its mostly register 2 register opcodes, the guts of the VM that handles all the opcodes is only 140 lines of code.. sweet.. once I add some memory handle stuff for the execing binary, i'll be set... hmmm...
now in the parser for the compiler is it BODMAS or BOMDAS... hmmm I havnt thought of that since high school...
BODMAS
(1+1) * 4 / 2 --> 2 * 4 / 2 --> 2 * 2 --> 4
BOMDAS
(1+1) * 4 / 2 --> 2 * 4 / 2 --> 8 / 2 --> 4
I remember both acronyms.. oops. one is wrong... I'm going loopy looking at it... both come out the same. uuuh.. i know there is a precedent order... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaargh!
anyway....
off to code more! finish the vm, then do the compiler later
i knocked out a virtual cpu.
register based
(16 registers,
reg0 is always zero,
reg15 is instusction pointer
reg14 is flags
reg13 is stack pointer)
is a risc design. all register to register except 1 command, which takes a 32bit number in.
there is only 13 commands, some things need to be 'emulated' with other commands (eg: push/pop)
i've already written the virtual machine in C so now all i need to do is write a c-like compiler...
i'm thinking of using it in my stuff as embedded scripting.
i did get a bit lazy, i could have implemeted sub/div/mul all as 'add' commands but i couldnt be arsed I'm strictly following RISC here...
and there is only 2 flags, overflow and equal...
since its mostly register 2 register opcodes, the guts of the VM that handles all the opcodes is only 140 lines of code.. sweet.. once I add some memory handle stuff for the execing binary, i'll be set... hmmm...
now in the parser for the compiler is it BODMAS or BOMDAS... hmmm I havnt thought of that since high school...
BODMAS
(1+1) * 4 / 2 --> 2 * 4 / 2 --> 2 * 2 --> 4
BOMDAS
(1+1) * 4 / 2 --> 2 * 4 / 2 --> 8 / 2 --> 4
I remember both acronyms.. oops. one is wrong... I'm going loopy looking at it... both come out the same. uuuh.. i know there is a precedent order... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaargh!
anyway....
off to code more! finish the vm, then do the compiler later