Weird problem with Game of Life
Posted: Fri Mar 18, 2011 2:02 am
Hi,
So, I'm making a Game of Life on a 16x16 LED Matrix display.
1 int = 2 bytes = 16 bits.
I am using an array of 16 int to use as "drawbuffer" (game_of_life_prev[]) and another one (game_of_life_next[]) to "create" the new cycle.
Because I haven't yet received the four 8x8 LED Matrix, I made a little VB program that receives the array of 16 int and prints each 16 bits of an int into a TextBox.
The Game of Life "algorithm" is programmed into the microcontroller.
I'll explain the VB program a bit more in details :
When the VB program is executed, it'll ask the user on which COM port the PIC is connected (mine is COM port 5).
You'll then see 16 TextBoxes for each int of the array.
There are also 2 buttons: a Start button and Receive button.
The start button sends a command ("<ST>") to the PIC so it'll know that the PC is ready to receive data.
The receive button sends a command ("<RY>") to the PIC so it'll know that it can send the data to the PC.
The VB program will then print each bit of each received int into the TextBoxes.
Note : This has already been debugged and works well.
The PIC program :
The PIC starts by initializing the USB Serial.
Once it's done it will wait for the "<ST>" command.
Next it enters the main loop and starts by writing the value of the next cycle into the previous one (game_of_life_prev[] = game_of_life_next[])
Next it'll wait for the "<RY>" command.
If it receives the "<RY>" command, it will send the 16 int.
This is done by getting the 4 nibbles of the int and sending its ASCII value between a start ('<') and stopbyte('>').
Ex.: if the integer is 0x34FD it will send "<34FD>" to the PC.
Once it has send everything to the PC it will make the "new cycle" into the game_of_life_next[] array.
The new cycle is done by checking every bit of the previous cycle and changing them "with the Game of Life rules" and writing everything into the new cycle array.
Checking, setting and clearing bits from each int is done with 3 defines in the supplementary code.
Then it'll loop back to the begin of the main loop and write the new cycle into the previous one (the drawbuffer).
Okay so now my problem :
The problem I'm having is that the value of only half of the bits in the int array changes with each cycle.
Only the last 8 bits (the lower byte) changes its value on each loop.
The first 8 bits (the higher byte) stays unchanged !
I have done a PC version of the Game of Life (it works great) and the "algorithm" is copy-pasted from there, so I don't understand why it has such a behaviour.
I thought maybe the boostC compiler doesn't like a for loop into another for loop ? (I haven't tried to change the for loops into while loops yet)
Right know I'm a bit tired of compiling-flashing-testing-failing-debugging-compiling-and so on. (It has been a week of trying and failing ...)
I hope someone can see something I don't and is messing up with the algorithm.
Attached are the PC Game of Life source code, the Flowcode Game of Life and the Excel file with the VB program.
I tried my best to add comments in the Flowcode Game of Life (I'm not the type of guy that writes comments everywhere in his code
)
So, I'm making a Game of Life on a 16x16 LED Matrix display.
I wanted to use as less memory as possible so instead of using 1 byte / LED, I used 16 int variables.PIC : 18F2455
XT : 4MHz
E-Blocks: LCD & USB
Jumpers : LCD set to default, USB set to A
Driver : Default driver when using the USB Serial component
Flowcode : v3
1 int = 2 bytes = 16 bits.
I am using an array of 16 int to use as "drawbuffer" (game_of_life_prev[]) and another one (game_of_life_next[]) to "create" the new cycle.
Because I haven't yet received the four 8x8 LED Matrix, I made a little VB program that receives the array of 16 int and prints each 16 bits of an int into a TextBox.
The Game of Life "algorithm" is programmed into the microcontroller.
I'll explain the VB program a bit more in details :
When the VB program is executed, it'll ask the user on which COM port the PIC is connected (mine is COM port 5).
You'll then see 16 TextBoxes for each int of the array.
There are also 2 buttons: a Start button and Receive button.
The start button sends a command ("<ST>") to the PIC so it'll know that the PC is ready to receive data.
The receive button sends a command ("<RY>") to the PIC so it'll know that it can send the data to the PC.
The VB program will then print each bit of each received int into the TextBoxes.
Note : This has already been debugged and works well.
The PIC program :
The PIC starts by initializing the USB Serial.
Once it's done it will wait for the "<ST>" command.
Next it enters the main loop and starts by writing the value of the next cycle into the previous one (game_of_life_prev[] = game_of_life_next[])
Next it'll wait for the "<RY>" command.
If it receives the "<RY>" command, it will send the 16 int.
This is done by getting the 4 nibbles of the int and sending its ASCII value between a start ('<') and stopbyte('>').
Ex.: if the integer is 0x34FD it will send "<34FD>" to the PC.
Once it has send everything to the PC it will make the "new cycle" into the game_of_life_next[] array.
The new cycle is done by checking every bit of the previous cycle and changing them "with the Game of Life rules" and writing everything into the new cycle array.
Checking, setting and clearing bits from each int is done with 3 defines in the supplementary code.
Code: Select all
#define SETBIT(pos, var) (var | (1 << (pos)))
#define CLEARBIT(pos, var) (var & ~(1 << (pos)))
#define GETBIT(pos, var) ((var & (1 << (pos))) && 1)
Okay so now my problem :
The problem I'm having is that the value of only half of the bits in the int array changes with each cycle.
Only the last 8 bits (the lower byte) changes its value on each loop.
The first 8 bits (the higher byte) stays unchanged !
I have done a PC version of the Game of Life (it works great) and the "algorithm" is copy-pasted from there, so I don't understand why it has such a behaviour.
I thought maybe the boostC compiler doesn't like a for loop into another for loop ? (I haven't tried to change the for loops into while loops yet)
Right know I'm a bit tired of compiling-flashing-testing-failing-debugging-compiling-and so on. (It has been a week of trying and failing ...)
I hope someone can see something I don't and is messing up with the algorithm.
Attached are the PC Game of Life source code, the Flowcode Game of Life and the Excel file with the VB program.
I tried my best to add comments in the Flowcode Game of Life (I'm not the type of guy that writes comments everywhere in his code
