Page 1 of 1

virtual cpu

Posted: Fri Apr 18, 2003 4:28 pm
by df
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 :)

Re:virtual cpu

Posted: Fri Apr 18, 2003 9:08 pm
by sonneveld
These will probably help you with the C compiler, if you haven't already visited.

http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

good luck. probably easier to write a compiler for a system that has a stack.

- Nick

Re:virtual cpu

Posted: Sat Apr 19, 2003 2:46 am
by df
yeah. lucky I have a stack :) register windows and stuff, i dont like. i'm a stack man... hmm..

nick, hows the agi compiler going?

Re:virtual cpu

Posted: Sun Apr 20, 2003 12:22 am
by sonneveld
besides pointers, everything is done.. with extra stuff like switches. Just need support in a stduio :)

- Nick

Re:virtual cpu

Posted: Sun Apr 20, 2003 3:59 am
by distantvoices
you guys are da(*)mn cool.
I am struck with management of real time processes. crap though that is i dont get it work. ****.