Hi!
Since i am a student, i go home for holidays, and usually i don't carry all my stuff all around the country, so home i only have left an old pc.
This time i tried to do something that might be helpful with programing mcu's, sbc, ect... Something like mini Java for mcu, and other platforms with little memory and low processing power.
My idea was to create a program that reads a bytecode program from a file, and executes the instructions.To make this bytecode execution as fast as possible, I made an array of pointers, so when the opcode is fetched, it loads the pointer from the array, and jumps to it(the opcode function is located at the pointer destination).
This should be a simple program, since it should be able to run on mcu, sbc, and other platforms...
Here is the code that i made so far(for x86 only(Dev C++)):
Code:
#include<stdio.h>
#include<stdlib.h>
#define ret() asm("jmp opscangtp")
//returns, fetches next instruction
#define fetch() tmp=pmem[pc++]
//loads next argument into the tmp variable
#define setop(N,O) (optable[N]=O)
//sets the operation label, to the selected opcode number[0-255]
int main()
{
unsigned char tmp;
int i,temp=0;
void *optable[256];
int retv=1;
char *pmem=(char*)malloc(65536);
char *retval=(char*)malloc(16);
unsigned long pc=0;
for(i=0;i<256;i++)setop(i,&&op_en);
setop(0,&&op_add);
setop(1,&&op_lol);
setop(2,&&op_and);
setop(3,&&op_or);
setop(4,&&op_scanf);
setop(5,&&op_if);
setop(6,&&op_sub);
setop(7,&&op_printretval);
setop(8,&&op_settemp);
void *v;
FILE * pFile;
pFile = fopen("myfile.bin","rb");
for(i=0;i<65536&&retv==1;i++)retv=fread(pmem+i,1,1,pFile);//loads the simulated 64k rom
fclose(pFile);
asm("opscangtp: ");
asm ("movl %1, %%eax;\n\rjmp *%%eax"
:"=r"(v) /* output */
:"r"(v=optable[pmem[pc++]]) /* input */
:"%eax" /* clobbered register */
);
op_add:
{
fetch();
*retval=temp+=tmp;
}ret();
op_lol:
{
printf("LOL\n");
}ret();
op_and:
{
fetch();
*retval=temp=temp&tmp;
}ret();
op_or:
{
fetch();
*retval=temp=temp|tmp;
}ret();
op_scanf:
{
scanf("%d",retval);
}ret();
op_if:
{
fetch();
if(temp)pc=tmp;
}ret();
op_sub:
{
fetch();
*retval=temp-=tmp;
}ret();
op_printretval:
{
printf("retval=%d\n", *retval);
}ret();
op_settemp:
{
temp=*retval;
}ret();
op_en:
free(pmem);
free(retval);
system("pause");
return 0;
}
Here is the content of the bytecode file myfile.bin (in hexadecimal)
Code:
04 08 01 07 06 01 05 02 70
The example will ask an input number(type 5).
This can't get too complex, because it won't fit in the small mcu/sbc rom, and i still don't know how the variables could be implemented. Any ideas?
And can this be modified to work with cc65?