Getting the T-Rex Endless Runner to work on a Component Tester

Intro

Some time ago I got one of these transistor testers on eBay for around 10$. It is quite useful, specially because my multimeter does not measure capacitance or inductance.  I did a samll write up about it in portuguese here. First thing I noticed was the popular Atmega 328p on the back of the board.

For some reason, I tried to calibrate it (there was no need) and messed up the values. Whatever was wrong, was inside the chip, specifically inside the chip’s eeprom. I found a way to read and program the chip, and then tried erasing the eeprom using avrdude, but could not get the measurements right. This made me think the board already comes calibrated from the seller. Because it was so easy to program the chip, I had decided that if I could not get it working I would just use it for anything else. I turns out I was just using a small capacitor when calibrating it, after using a capacitor of higher value than the recommended it worked just fine.

Anyways, the atmega328p used on the board it is the same present in many duinos out there(UNO, Nano, Mini, a thousand clones.. etc), so it got me thinking “Why something so hackable is not being hacked?”. I mean, it has an LCD screen, a sort of button, battery connection, and a SPI port available.

Following, I will explain how I got the component tester to run a game similar to the T-Rex game. It is far from good, but I learned some tricks along the way. As this is my first try at something game/graphics stuff, I had a lot of guessing. Suggestions are welcome!

First, some reverse engineering

Started this project last July. But only now I got free from college(summer in Brazil) and had time to finish and document it.  Some information here can be just a bit inaccurate, but don’t worry I got a sort of good memory :)

I knew the board was based on a open source project by Markus Frejek. But as there were so many versions I did not believe that the original repo would contain files for my tester, so I set out to find out the LCD reference and pinouts.

Searching on-line I read that a LCD that matches the one (in size and look) on the board was based on the ST7565 controller, but first I had to know which pins of the atmega connected to the LCD.

By simply using a multimeter and the continuity test, I was able to find out that the LCD was connected to PORTD of the microcontroller. But how would I know which pin from the microcontroller is connected to each pin of the LCD(IO,RESET, CLK and so on) if I could not find its pinout?

The best way, I thought, would be having a look at the disassembly of the current firmware, that is, if it was available.The flash was not protected and I could read the flash without problem. I used ODA to disassembly the binary. There is a ‘reversed’ ISCP on the board, so I did not have to solder any wires in order to read the chip. See in the picture below how I connected my trusty USBASP to the Atmega.

Before anything, I dumped the original firmware. I just burn this file to the board again when I want to use it as a transistor tester.

And simply used ODA for disassembling the file.

But now that I have the old firmware disassembled, what were I looking for here? Quite easy, by looking for writes to the address of PORTD, eventually I would find the pins used as a the SPI. The mega328p hardware SPI is in PORTB, so the GPIOs on PORTD were bitbanging the data to the LCD as a software SPI.

Lets have a look at this table from the microcontroller Datasheet

From the table, PORTD address is 0x0B when not using LD or ST istructions. Searching this address in the disassembly I found the following code:

From this I got that:

And from other parts of the disassembly:

That is awesome, and I got kind happy for figuring it all by myself, but some days later I found the following piece of code in file config.h from the open source repo:

Great, all the previous work for nothing. As my friend Márcio used to say “A lot of work is wasted”.

Getting the LCD to work

Ok, I had the pinout of the LCD, and its reference. I searched for a library for controlling it and found Adafruit’s library repository here.

I could not get it to work at first, but after setting the brightness right I could see some image on the LCD, but it was reversed.

Adafruit’s library was written for a upside down module (see the picture below). For using it correctly with the transistor tester I would have to modify it.

 

Adafruit ST7565 LCD. Available here.

See that the cable is at the top  of the LCD on the tester.

I made the necessary modifications for using the library with the T3 transistor tester, for both C and Arduino. I put everything in a repository on github.  Now that I had a video library, I could start to work on the game.

Why T-Rex?

First of all, it is an almost monochrome game (the clouds are of a lighter gray), so it would be just fine on the LCD. Second, I would say it is kind of simple. I mean, I don’t know any game programming and got it working(sort of). Also, it is popular and easily recognizable. Finally, dinosaurs are super.

I found a github repository with he t-rex runner game extracted from chromium. From the files there I could get the sprites and have a look at the game code, to see how to reproduce it. Many thanks to Wayou for sharing.

Graphics

I got the following image from the repository mentioned above. Then, I resized it so that the eye of the rex would be one pixel in size.

 

 

Then I had the exciting task of cutting all the dino, ground and cacti sprites. I used gimp for cutting and converting them to bitmap.

 

Then, I used Adafruit’s repository recommended tool, bmp2glc for converting each of the bitmaps to .h files with byte arrays.

Game Logic

I believe the game can be summed up in looping through:

  • Calculate the new positions  of the sprites
  • Check collision between cacti and rex
  • Draw the sprites to the LCD
  • Update score

These steps are ok to work on. But after getting some sprites to the screen, I noticed there were some serious problems with speed. I could not even test at that speed. So we better have a look at that first.

Performance issues

I had two problems I could not fix:

First, the Atmega was running at 8MHz – This is 40% of the maximum clock speed that the mega328p can have. I still wanted it to work as a component tester, so I would not change the crystal.

Second, the display is hooked up to a soft SPI – This means that instead of transferring a byte at just one clock cycle, the micro has to shift some data, test a bit, write to the data and clock GPIOs… all this eight times per byte.

All the other problems were related to the LCD library or the game itself. Without Adafruit’s library I would not even work on this project, it was quite useful and easy to use, but just needed some tweaks for best performance.

I saw some code that could be reworked:

The library uses a buffer, first we write sprites to the buffer and then write the buffer to the LCD.

So to write a bitmap to the buffer, it is done bit by bit by calling setpixel() every pixel. I modified this to write a byte already:

If a byte from the bitmap is aligned to a page on the LCD, just copy it, if not, break it into two bytes and write them to the buffer. I got smart here and aligned the sprites in the game, so the only sprite that needs the ‘else’ above is the dino,  but only when jumping.

Another problem was that to update the screen, it was necessary to write the whole buffer to the LCD. At first I thought of writing only the pages modified, but then I realised I could create a new function to write only part of the buffer, giving the coordinates to the function.

The write_buffer() funtion:

A new function, to write only part of the buffer:

Then I could manage the LCD updates by just giving the coordinates of the sprites.

Another small thing, the ST7565 does not have a command for clearing the screen. And the library has a function for this, but it just writes 1024 zeros to the screen. Still, I got from one of the example codes that is best just writing the negative of the sprites to same place than clearing the whole screen.

If made right, the positions of the rex should be calculated on real time. But calculating square roots, powers and stuff in a 8 bit micro controller would be a pain and take so long. So the trajectory of the jump is fixed. Instead of calculating the parable,  the code just acess a table with the next y value for the dinosaur.

Game Logic (Cont.)

Ok, I said before that the game would be summed up in a loop to:

  • Calculate the new positions  of the sprites

As it would be really slow to calculate the trajectory of the dino when jumping, by using speed and aceleration parameters. I opted to just using a table with fixed y values. When jumping the rex position is gotten from the following array:

The cacti and ground, however, are just having their x values decremented every frame.

  • Check collision between cacti and the rex

Here, I think the usual is using hitboxes. But in this case,  I figured out that by when writing a bitmap to the buffer, I could check if there was at least a pixel, really a single pixel, where the bitmaps colided. It works like this, before I write a byte to the buffer, I and (&) the previous byte with the new byte. Only when a bit is set at the same position in both bytes, we have a collision.

The drawbitmap() function we saw before becomes:

And in the game code I have something like:

  • Draw the sprites to the LCD

I just use the library functions to write the bitmaps to the LCD. However, because of performance and ghosting reasons, the bitmaps are only written to the LCD when moving position of the object(like the cacti) or changing bitmap (like when the dino changes the leg touching the gorund).

  • Update score

I use the EEPROM to keep the hiscore when the the board is turned off. I looked for a place not used by the old firmware, so I could change the flash without worrying about the EEPROM.

  • More

I used a unconnected ADC pin as a source of randomness for srand(), this means I have to wait for a ADC conversion. I also think there’s some math in srand(and I also use % to top the number returned) that slows things just a bit, but I am just guessing, I haven’t seen the code. I tried just getting a number from the timer in CTC mode. but it is just awful.  I need to study some of this when I have time.

I put a lot of time in fixing the code, removing unnecessary stuff, only writing to the LCD when really needed, making the library faster. At first I could not even see the problems, but with time I got more and more aware of ways of not letting the game waste time doing unnecessary tasks.

There is some ghosting characteristic to the display. There is no way to fix this, but I guess it is okay, even the Game Boy had some :)

 

The LCD takes some time to change the stage of a pixel, so between frames there is some visible ghosting. It gets better at low fps.

About input, the button present on the board is used to start up the microcontroller. Every time it is pressed, the LCD back-light goes off. I use another button connected to terminals 1 and 3 of the ZIF socket to allow input without turning off the back-light.

Button connected between 1 and 3 in the ZIF socket

Video

You can see a video of the thing below.

The cacti look better in real life. I guess because of the persistence of vision effect. The component tester would be better for slower games. I guess arkanoid or space invaders would be pretty cool, but the back-light would have to be turned off in order to use both buttons.

Conclusions

I spent more time in this project than I wanted to, so it is not finished yet. It is missing the pterodactyls, for example, and the way cacti repeat in the original game. It is just I am fed up for putting to much time in this, but I will probably work on it in the future with another microcontroller or LCD. The code is available in a github repo here.

This was mostly a way of training C programming, and I am quite satisfied with the results. I got to know how manipulate bitmaps, got more used to pointers, structures and some other small things. Speeding up things was challenging, but quite fun.

I  would like to thank Rafael for his interest in the project, we discussed some of the ideas that I implemented here.

Well, I think that is it for now.

Thanks for reading. See ya in a next post o/

 

Robson Couto

I am a Electrical Engineering student. I should be studying motors or transmission lines right now, but instead I am probably reading about computer history, microcontrollers, buses and protocols. My life is basically I having trouble because of this.
God help me please.

One thought to “Getting the T-Rex Endless Runner to work on a Component Tester”

Leave a Reply

Your email address will not be published. Required fields are marked *