Adding words to openbios

In this example, we are going to implement a forth “word” in C. There are two arrays that need to be looked at. One is located in the file openbios-devel/kernel/bootstrap.c. The other array is located in the file openbios-devel/kernel/forth.c. The two arrays are used to map a C function to a forth word.

The array in bootstrap.c is where the name of the forth word would go. The array in forth.c is where the corresponding C function would go. When adding to these arrays, make sure to add at the end of the list. This ensures everything continues to run smoothly.

Here is an example on adding to the dictionary. Say you want to add a word called dog. You can define the C function like this:

static void dog(void)
{
printk(“bark bark!”);
}

Add this function to a .c file. Knowing which file to pick can involve a lot of trial and error. In this example, we will use the file openbios-devel/kernel/forth.c. Be sure to include the static keyword before in the function declaration, or OpenBIOS won’t build successfully.

Now we add the word dog to the end of the forth word list. Open the file bootstrap.c, then find the array called wordnames. When adding the word dog, be sure the name of the word is surrounded in double quotes. Here is how the array should look:

Next we add the C function name to the end of the function list. Open the file forth.c and add dog (no quotes this time) to the end of the list. You may want to add a comment to the right indicating the word this c function corresponds to, but this isn’t necessary.

Congratulations, you have just taken your first step to improving OpenBIOS.

Advanced example:

Suppose you want to make a forth word that would give you access to a PowerPC CPU register. To do this you can use inline assembly language to access the register from C. Then use the bind_func() function to make this C function available to forth.

In this example we implement a word called fpscr@ that will push the value of the FPSCR onto the top of the stack.

In the file arch/ppc/qemu/init.c add this code right above the arch_of_init() function: