Small RISC emulator

I want to build a virtual machine in the game and wondered if anyone knew about a really simple virtual machine (I thought RISC / PIC was close to what I wanted), which are commonly used for embedded projects like robot control, motors, sensors, etc. My main task is to write a compiler / assembler if I give up my own. It would be nice for me to use tools that are already in or in its simplest form, just a C compiler that can compile it: -p.

I really don't want to reinvent the wheel here, but I also need thousands of them that work around the virtual world, so they should be as fast and fast as possible. As one person has already said, I also don't care about real-world issues like time and buses, and all these funny things. I think that their virtual clock will be limited rather slow; and ultimately I’ll probably have to learn about built-in compilation to get them working even faster, but for now I’m just building prototypes to get a general proof of concept.

As an input, I plan on remote, light, material and sensor sensors mounted around a cylindrical body (16, maybe 32 of them), and then just 2 engines for directional output to control the wheel on each side. in fact, the processing will not be too intense, and the world will be quite simple, so that the machine does not have to throw up a lot of computing power for simple tasks.

As far as memory is concerned, I would like them to be able to store enough data to remain alone for several days without interference to create maps and collect statistics. I don't like that 8 bits will cut it down for processing or memory, but 16 bits will definitely be a rival. 32 and 64 bits would just push it, and there would in no way be more than 1 mb of memory - probably closer to 256-512 thousand (Bill said that 640k would be enough, so why I can not!)

+2
source share
5 answers

If you want something embedded in the real world, one of the most used RISC embedded microcontrollers is the PIC family. Google offers several emulators, but I don’t think the source is available to most.

Another feature is QEMU, which already emulates several varieties of ARM.

and, of course, if you are not interested in emulating a real device, it is much easier and more efficient to work with you. using just what you need and not getting into a mess of status flags, overflow bits, limited bus widths, RAM timings, etc.

+2
source

I wrote Wren for a friend who wanted the VM language to work on an embedded controller with approximately 16K of RAM. (But it allows up to 64k per process in code, as written.) It includes a compiler for a dumb little programming language. All this, quite simple, and did not see much use, but this is exactly what you described in your first paragraph.

+6
source

The FORTH virtual machine is about as simple as them. 16-bit address space (usually), 16-bit data words, two stacks, memory. Loeliger's “Threaded Interpretive Languages” tells you how to build a FORTH interpreter on the Z80.

+5
source

If you want to simply, take a look at Manchester Sign I. See page 15 of this PDF . The machine has 7 instructions. It takes about an hour to write an interpreter. Unfortunately, the instructions are quite limited (which is why a largely complete specification of the machine can fit on one page).

The javier approach associated with your own is very pragmatic. Designing and creating a tiny machine is a two-day task, if any. A few years ago, I built a tiny virtual machine for a project, and it took me two days to write a virtual machine using a simple visual debugger.

Also - should it be RISC? You can choose, say, 68K, for which there are open source emulators , and 68K is a well-understood target for gcc.

+1
source

Many people who write game programs and other applications implement the language in the application to allow users to write small programs.

As far as I can tell, the most popular built-in language in the very rude, most popular in the first order (although “more popular” doesn't necessarily mean “better”) seems to be:

You can check Gamedev StackExchange, in particular questions such as "How to add scripting language to the game?" .

You might want to check out some of the questions here on StackOverflow with embedded language tags; such as "Choosing an Embedded Language , " "What is a Good Embedded Language That I Can Use for Scripting Inside My Software?" Alternatives to Lua as an embedded language? "Which game scripting language is better to use: Lua or Python?" , and etc.

Many implementations of these languages ​​use a kind of bytecode internally. Often two different implementations of the same high-level programming language, such as JavaScript, use two completely different bytecode languages ​​inside ( a ). Often, several high-level programming languages ​​make up the same basic bytecode language — for example, the Jython implementation for Python, the Rhino-style JavaScript implementation, the Jacl Tcl implementation, JScheme, and several other Schema implementations and several Pascal implementations; all compiled into a single JVM bytecode.

the details

Why use a scripting language rather than interpret any language on a hardware computer?

Why "alternative hard and soft layers" ? To get simplicity and speed up development.

faster development

People usually work faster with scripting languages ​​than with compiled languages.

Getting the initial prototype work is usually much faster - the interpreter processes a bunch of things off-screen that the machine language forces you to explicitly write out: setting the initial values ​​of the variables to zero, the prolog subroutine and the -epilog code, malloc and realloc subroutine, as well as free and associated memory management, increasing the size of containers when they are full, etc.

As soon as you have the original prototype, adding new functions is faster: scripting languages ​​have fast edit-run-debug cycles, since they avoid the “compilation” phase of the edit-compile-run-debug cycles of compiled languages.

Simplicity

We want the embedded language language to be “simple” in two ways:

  • If a user wants to write a little code that performs some conceptually trivial task, we don’t want to frighten this person with a difficult language that requires 20 pounds of books and months of training to write “Hello, $ USER” without buffer overflow.

  • Since we are introducing the language, we want something to be easy to implement. Perhaps a few simple basic instructions, we can knock out a simple interpreter on the weekend and, perhaps, some already existing compiler, which we can use with minimal setup.

When people create processors, hardware limitations always limit the set of instructions. Many conceptually “simple” operations — things that people constantly use — ultimately require a lot of machine language instructions to implement.

Nested languages ​​do not have these hardware limitations, which allows us to implement more complex "instructions" that do things that (for humans) seem conceptually simple. This often makes the system simpler in both directions mentioned above:

  • People who write directly in the language (or people who write compilers for the language) end up writing much less code, spending less time on a single click of the code, debugging it, etc.

  • For each such operation of a higher level, we shift the complexity with the compiler to implement the instruction inside the interpreter. Instead of (you write the code), the compiler breaks some higher-level operations into a short loop in the intermediate language (and repeatedly steps over this loop in your interpreter at run time), the compiler emits one command in the intermediate language (and you write the same sequence of operations in your interpretive implementation of this intermediate “instruction”). With all the intensive processor tools implemented in your compiled language ("inside" complex instructions), extremely simple interpreters are often more than fast enough. (Ie, you avoid a lot of time building a JIT or trying to speed things up in other ways).

For these reasons and others, many programmers use the "scripting" language as their "embedded language."

(Now I see that Javier has already recommended "using the built-in scripting language", so this has turned into a long debate about why this is a good alternative to interpreting a hardware computer language and indicating alternatives when one particular scripting language does not seem appropriate).

+1
source

All Articles