Code: Select all
'It simulates my own CPU with 2 registers, 5 opcodes and 256bytes of rom - have fun :)
dim shared as ubyte r0,r1,prgctr,rom(256),exitflag
function hexconvert(value as ubyte) as string
if value < 16 then
return 0 & HEX(VALUE)
else
return hex(value)
end if
end function
sub cmsg(msg as string,adr as ubyte) ' a simple string to rom compiler
for i as integer = 0 to len(msg)-1
rom(i+adr)=msg[i]
next
end sub
sub wr(value as ubyte) 'write a data into the rom and incrase the progrommcounter
print hexconvert(value) & " " ;
rom(prgctr)=value
prgctr+=1
end sub
'this subs represents the microcode
sub c_print_r0
wr 0
end sub
sub c_set_r0(value as ubyte)
wr 2
wr value
end sub
sub c_set_r1(value as ubyte)
wr 3
wr value
end sub
sub c_inc_r0
wr 1
end sub
sub c_cmp
wr 4
end sub
sub c_jump(value as ubyte)
wr 5
wr value
end sub
'firmware programming beginns here..
print "COMPILING..";
cmsg ("HELLO WORLD",100)
c_set_r0 100
c_set_r1 111
dim as ubyte jumpadr = prgctr
c_print_r0
c_inc_r0
c_cmp
c_jump jumpadr
print "DONE. (SIZE :";prgctr;" BYTES)"
'vm starts here
prgctr = 0
do
'microcode starts here
select case rom(prgctr)
case 0
print chr(rom(r0));
case 1
r0+=1
case 2
r0 = rom(prgctr+1)
prgctr+=1
case 3
r1 = rom(prgctr+1)
prgctr+=1
case 4
if r0 =r1 then exitflag = 1
case 5
prgctr = rom(prgctr+1)-1
end select
'microcode ends here
prgctr+=1 'incrases the progrommcounter
loop until inkey <> "" or exitflag = 1
locate csrlin+1,1
print "PRESS A KEY TO EXIT"
sleep