📜 ⬆️ ⬇️

Virtual machine on ESP8266 to run games

VM, written by the uncertain hand of the humanities in the Arduino programming environment using bydlokod and bicycles. And then there is a compiler for it from a C-like language, written in JavaScript using the same methods. Yes. You can already hurry in the comments, throwing stones. Well, those who are still interested, I invite you to continue reading.

Trolleybus from the loaf

In general, my hand-made article was already a little illuminated by a respected tormozedison here . But at that time there was more likely a spherical prototype in a vacuum. And now I have a device for the work of the RomanS, which he graciously provided me for the experiments completely free of charge. This device is called ESPboy. It differs from other handicrafts in compactness and expansion slot, using the chip MCP23017. Here you can learn more about it.

If anyone is interested in how I even had such a strange thought, like a virtual machine on a microcontroller, I can read it under the spoiler.
')
Prehistory
Once at school, I became interested in gamer from yoyogeyms, and, like many, riveted on him, do not understand what. These crookedly written and poorly drawn demos could still boast to friends, but on the Internet they sank among similar ones. I realized how hard it is to write even a simple game, let alone a serious one, in which it would be possible to rob the cows. But the desire to write games is not lost. When I got a mobile phone, I discovered Midlet Pascal and had fun with it for several years. After all, I understood that writing a game for a weak device is easier than for its more powerful big brother. At least, one could always speak not about curved hands, but about the limited capabilities of the platform. However, the buttons were replaced by sensors. Another chance to conquer the world was missed. Yet, at some point, I realized that I could revive my dreams using a microcontroller.

I began by writing a game under the Arduino Uno, where without it. Then she was my only one, and I was very worried about her performance. I tried to flash as little as possible. But the path of the rake goes in small steps. Each small editing of the code made it necessary to flash again and again. And I decided to write a stack virtual machine. After all, onboard is a huge two kilobytes of RAM for code and data bytes. But how slow she was, but for some reason she constantly lacked memory. Probably, the thing is that all my bikes were with square wheels and drove slowly. Then I decided to write a chip-8 emulator on the Arduino mega just come from China. Of course, then I wanted to write for her the gameboy emulator, the first playback and some supercomputer easier. As a result, I realized the main thing. The chip-8 virtual machine was very simple, overly simple. So much so that when writing my multiplication or division games you had to write a separate slow subroutine. So you need to write your VM, getting rid of the fatal flaw. At this time, several esp8266 arrived, with their space 160MHz.

If it seems to you that the virtual machine is a waste of resources, then I also rewrote my chip-8 emulator for it. It turned out a virtual machine in a virtual machine on a microcontroller. Perhaps you can go further and write a turing machine on chip-8.

Technical characteristics of ESP Little Game Engine


The virtual machine contains 16 registers of 16 bits each, the null register is a stack pointer. Each instruction is two-byte, some instructions contain two bytes of data after themselves. 64KB addressable memory. In the case of the ESP8266, 20KB is available. The program can be downloaded from SPIFFS and UART. If desired, you can add a download from a memory card or via WiFi. In addition to the usual arithmetic instructions and instructions for moving data, there are separate instructions for working with sprites, screen and sound. The screen size is 128 by 128 pixels. With 16 colors per pixel, the screen takes up 8KB of memory, and as many takes up the buffer for drawing sprites and particles. Although the TFT_eSPI library used by me is capable of updating the screen more than 60 times per second, I had to limit myself to 20 frames per second. Otherwise, there was not enough CPU time for the virtual machine. You can draw tiles and 32 sprites up to 128x128 pixels in size with the possibility of rotation and mirroring. To save memory, you can use single-bit images or RLE compression. There is a simplified physics: collision detection sprites with sprites and tiles, collision resolution, gravity. The screen is updated line by line only if the line has a pixel change. The speed of a VM, depending on how many lines are drawn in a frame, varies from 100 thousand to 900 thousand operations per second. You can use different color screens, there is a soft stretching of the image to the desired proportions.


Some of the games written by me, you can see here .

Along with the VM for ESP8266, I wrote a JavaScript emulator for the browser. In order not to edit the bytecode manually, a simple assembler was added, based on my experience with the assembler for MOS6502. Then I decided to add a higher level language. Yet my main task was to quickly write simple games, and not long debugging of the assembler code. It seemed to me that it would be simpler to write the compiler, than to add LLVM. And I wrote it in JavaScript, because I know it not as bad as other languages. At the moment, it’s far from supporting C standards and when compiling it is easy to face an incomprehensible error in an incomprehensible place. But he is fast, because it takes less than 2000 lines.

And now to the question, why did I even write all this here? Already, you can write and run games. However, perfection is still far away. If anyone wants to help me in the finalization of ESP LGE, or write my own game, then I will be very happy. If you thought my idea was interesting, and you want to learn more, then I will be happy to answer your questions. I warn you, the code for the Arduino is quite difficult to read. Partly because I'm self-taught. Partly due to the fact that I tried to reduce the number of function calls, to increase the speed of work. As a result, many features contain huge footcloths code. So do not let go of the screen for pregnant women and children. Gradually, I will try to fix it and improve readability.

And for those who read, an example of the game. It takes less than one hundred lines and less than 1KB in compiled form.

int stickCount; char key,previouseKey,takenSticks; void redraw(){ int i; //   setcolor(2); //   for(i = 0; i < stickCount; i++) line(22 + i * 6, 74, 22 + i * 6, 84); //   setcolor(11); //  for(i = stickCount; i < 15; i++) line(22 + i * 6, 74, 22 + i * 6, 84); //     setcolor(1); //   delayredraw(); } void playersMove(){ //    ,     .  while(key == previouseKey){ key = getkey(); } while(key != KEY_LEFT && key != KEY_DOWN && key != KEY_RIGHT){ key = getkey(); } if(key & KEY_LEFT){ takenSticks = 1; }else if(key & KEY_DOWN){ takenSticks = 2; }else{ takenSticks = 3; } printf("%d, ", takenSticks); stickCount -= takenSticks; previouseKey = key; } void computersMove(){ if(stickCount % 4){ //   ,    takenSticks = stickCount % 4; }else{ //      takenSticks = 1 + random(1); } stickCount -= takenSticks; printf("%d, ", takenSticks); } void game(){ // stickCount = 15; clearscreen(); //       gotoxy(8,0); puts(""); gotoxy(2,1); puts(" 1,2  3 .  ,   . :\n"); // 27,25  26   printf(" %c 1 %c 2 %c 3", 27, 25, 26); gotoxy(0,12); redraw(); while(1){ playersMove(); if(stickCount <= 0){ gotoxy(3,8); puts(" "); return; } redraw(); computersMove(); redraw(); if(stickCount <= 0){ gotoxy(3,8); puts(" "); return; } } } void main(){ while(1){ game(); //  settimer(1,1000); while(gettimer(1)){} while(getkey() == 0){} previouseKey = key; } } 

You can immediately and test . Follow the link, then click compile, then run. If you want to learn more about the possibilities of IDE you can read the tutorial . Thanks for attention.

Source: https://habr.com/ru/post/459714/


All Articles