Pages

Thursday, August 21, 2014

[Minix][Tutorial 3] Setting up a project with some useful scripts

In the previous tutorials we have set everything we need to develop programs for minix:
  • Minix running on a virtual machine through VMware Player;
  • Eclipse, with Remote System Explorer.
From now on, I think people following these tutorials will benefit more if I develop a project from scratch, so that is exactly what I am going to do.

In this tutorial I will help you to create and set a project. I will also teach you how to create some useful scripts which we will be using a lot during the project development.

So, let me think of a simple project which will be fun to develop... What about Flappy Bird? It makes use of graphics, keyboard, mouse to click the menu buttons, timer, and could easily use more stuff. I think it is a good idea! Let us start?

Create a new project folder

First of all launch Minix, you know the drill:

login: lcom
password: lcom

After logging create a new folder, this will be our project directory. I called mine flappy-nix.



Now open eclipse and open the RSE perspective. We already have a connection to minix which we created on the previous tutorial. If you open My Home (use the same login and password), the folder we have just created should be listed.



Create the project folder structure

Now is a good time to create our project folder structure. So go ahead and create the following folders through RSE:
  • conf - this will contain a configuration file needed to install the program
  • res - this folder will contain all the game resources, such as images
  • src - this is where our source code will be stored



Next, create main.c inside the src folder.



Create the Makefile

Also create the Makefile inside src.



The Makefile is like a set of rules to compile our project. It is a file with no extension, and has to be exactly named Makefile - with the first letter capitalized.
Paste the snippet below to your Makefile.
COMPILER_TYPE = gnu

CC = gcc

PROG = flappy-nix
SRCS = main.c

CCFLAGS= -Wall -O3

DPADD += ${LIBDRIVER} ${LIBSYS}
LDADD += -llm -ldriver -lsys

LDFLAGS += -L.

MAN=
BINDIR? = /usr/sbin

.include <bsd.prog.mk>
.include <bsd.gcc.mk>
Notice that PROG should have the same name as the project folder. You should also remember that every time you create a source file they should be declared in SRCS (only the .c files, not the .h). That's why SRCS = main.c. We will be adding more source files later.

We will also need a low memory library. Download it and drag it to the src folder.



Create amazing scripts

Trust me, scripts are great! They save a lot of time and typing. I will show you how to create three scripts you will fall in love with.

To create a script right-click the project folder, select New > File and then type the name of the script.

install.sh

This script does the following:
  • Installs the system configuration file required to execute it with the right permissions;
  • Copies the res folder to a safe and convenient destination;
  • Modifies the remaining scripts permissions.

Paste this snippet to install.sh:
cp conf/flappy-nix /etc/system.conf.d
mkdir /home/flappy-nix
cp -vr res/ /home/flappy-nix
chmod 777 compile.sh
chmod 777 run.sh

compile.sh

This script compiles the project. Paste the snippet below to compile.sh:
cd src
make clean install
mv flappy-nix ../
cd ..
strip --strip-all flappy-nix

run.sh

This script executes the project. Paste the following snippet to run.sh:
service run `pwd`/flappy-nix
After all these copy/pasting, you should have exactly what is shown on the picture below.



Why did we bother to create these scripts?
Well, because every time we need to compile our project (for example), instead of typing all the stuff we pasted to compile.sh by hand, we will just have to type: sh compile.sh! Isn't that great? You want to run the project? Easy! You just have to type: sh run.sh.

We need to do one more thing: create the system configuration file. This is a must!
Right-click the conf folder, select New > File and type flappy-nix. Then paste the snippet below.
service flappy-nix {
    system
        DEVIO
        IRQCTL
        UMAP
        INT86
        PRIVCTL
        READBIOS
        IOPENABLE
        ;
    ipc
        SYSTEM
        rs
        vm
        pm
        vfs
        ;
    io
        40:4      # Timer
        60        # KBC
        64        # KBC
        70:2      # RTC
        2f8:8     # COM2
        3f8:8     # COM1
        ;               
    irq
        0         # TIMER 0 IRQ
        1         # KBD IRQ
        3         # COM2 IRQ
        4         # COM1 IRQ
        8         # RTC
        12        # AUX/MOUSE IRQ
        ;       
    uid
        0
        ;
};



Finishing up

We have already done a lot during this tutorial! I think it is enough for now. So let's just check if everything is working as it should.

Open main.c and write a simple hello world program, such as the following:
#include <stdio.h>
int main(int argc, char **argv) {
    printf("Hello, minix!\n");
    return 0;
}
Now go to minix, go to the project folder and enter superuser mode by typing the command: su.

Then execute the scripts by the following order:
  • install.sh
  • compile.sh
  • run.sh

It should give an error, but that's ok for now as long as you see the "Hello, minix!" message.
Here is a screenshot of my output for comparison:



Killing a service

If for some reason you run your program again and you get something like:

Request to RS failed : Resource busy (error 16)

That means that the program did not finish correctly and it's service is still running in the background. To kill/stop it, just run the command:

service down name_of_the_service_to_be_killed

In our case, it would be:

service down flappy-nix

This will kill the service and thus enable you to run it again. But do not get away with it this way, you should look for what is causing the problem and fix it.
This is a very useful command for the entire development of the project.

Back to index

Click here to go back to the index post.

No comments:

Post a Comment