I think that is a good basepoint to write an more complex emulator.
Code: Select all
dim shared as ubyte r0,r1,exitflag,prgctr,ram(256)
sub cpu
do
'case x = opcode
select case ram(prgctr)
case 0
print chr(ram(r0));
case 1
r0+=1
case 2
r0 = ram(prgctr+1)
r1 = ram(prgctr+2)
prgctr+=2
case 3
prgctr=ram(prgctr+1)-1
case 4
if r0 = r1 then exitflag = 1
case else
print "UNKNOWN OPCODE (";ram(prgctr);") @ ";prgctr
end select
prgctr+=1
loop until exitflag = 1
end sub
'compiler instructions
sub wr(value as integer)
ram(prgctr)=value
prgctr+=1
end sub
sub compile_print
wr 0
end sub
sub compile_inc_r0
wr 1
end sub
sub compile_setup (startadr as ubyte,endadr as ubyte)
wr 2
wr startadr
wr endadr
end sub
sub compile_jmp (adr as ubyte)
wr 3
wr adr
end sub
sub compile_compare
wr 4
end sub
sub compile_string(msg as string)
for i as integer = 0 to len(msg)-1
wr msg[i]
next
end sub
'lets compile the firmware
dim as string message = "Hello World"
compile_jmp 2 + len(message)
compile_string message
compile_setup 2,1 + len(message)
dim as ubyte mainloop = prgctr
compile_print
compile_compare
compile_inc_r0
compile_jmp mainloop
'lets run :)
prgctr = 0
cpu
sleep