Thursday, October 13, 2005

Day 163

[reading: Geoff Ryman, "The Unconquered Country"]

I've been writing the Preferences dialog for my Palm version of MineSweeper today, so now the game is basically code complete—I just have to play with it and weed out the bugs.

However, getting the Preferences dialog up and running did expose a slight problem that I'm still not sure how to solve. The Preferences dialog allows change of board size and number of mines, and for a large, sparse board this causes a problem in the code that automatically exposes connected blank squares: blowing the stack. The straightforward implementation of this code involves recursion, and the default PalmOS stack is only 4k. At the moment, I've stripped the code down so that nothing gets passed as a parameter to the recursive function (it turns out all the state can be kept in static global variables; the return address is the only stacked state needed) and increased the stack size (to a whopping 32k) and that seems OK for all but the most pathological cases.

Given that I'm having difficulty finding a web page that describes how to increase the PalmOS stack size, for future references here's how: add the following resource and make sure the compiled version (something like pref0000.bin) comes before the application binary in the build-prc link line. The line in red is the stack size as a 32-bit integer split into its (big-endian) constituent bytes.
    HEX "pref" ID 0
      0x00 0x00
      0x00 0x00 0x80 0x00 
      0x00 0x00 0x10 0x00

I remember a time, when I first started programming, when I found recursive code very hard to understand; it's interesting to see that this has reversed, and I now struggle to express some things non-recursively.

No comments: