Conway's game of life

By bverhage

Supporter (9)

bverhage さんの画像

17-09-2021, 15:40

Hi All,

Recently I've been picking up on coding for the MSX again. It's been ages since I did any coding for MSX, but getting back into this is fun.

As a start project I want to do an implementation of Conway's game of life. Since I want it to be fluent I guess assembly is the way to go. I've started some coding and I am getting at least somewhere. But i'm currently struggling with the part where to draw pixels on screen. Basically my idea would be to have a playing field as big as the screen. For that I need to draw the screen pixel by pixel. Looking up information on programming graphics gives me information about sprites and bitmaps. So I'm a little lost:)

Anyone having experience in this? Is this possible to make a fluent (read fairly fast) implementation in your opinion? Any code snippets I could have a look at and learn from regarding putting individual pixels on screen?

Looking forward to your answers!


By santiontanon

Paragon (1833)

santiontanon さんの画像

17-09-2021, 16:41

Welcome to the forums bverhage!

Assuming you are talking about first generation MSX, the closest to a "bitmap mode", which I think is what you need is to use Screen 2. In Screen 2, the screen is made out of "tiles", where each tile is a block of 8x8 pixels, defined by two blocks of 8 bytes:
- 8 pattern bytes: where each byte represents a row of 8 pixels, and each bit in each byte is one pixel.
- 8 attribute bytes: where each byte also represents a row of 8 pixels, and contains two colors (4 bits per color).
- a 0/1 in the pattern bits switches between using one or the other color specified for that block of 8 pixels in the attribute bytes.

So, assuming you want a black/white screen, and just turn on/off pixels, this is what I would suggest:
- Switch to screen 2
- Initialize the 768 bytes of the"name table" to 0, 1, 2, 3, 4, ... 255, 0, 1, 2, ..., 255, 0, 1, 2, ..., 255 (this basically sets screen 2 in what some people call "bitmap mode").
- Then set all 6144 bytes of the "attribute table" to 0xf0 (black and white)

For an example of how to do all three steps above, you can see the "set_bitmap_mode_a_color" function here:
If you do:

    ld a,2      ; Set screen 2
    call CHGMOD  ; CHGMOD = #005f
    ld a,0xf0
    call set_bitmap_mode_a_color

Then, you will have the srceen set up

- Finally, now you can just write arbitrary values to the pattern table, and setting bits to 1 or 0 to change colors from black to white or vice-versa
- If you just want to switch one single bit, then you need to read the byte in the position you want to write on the screen, change the one bit, and write it back.
- You can see examples in the "draw_white_pixel" and "draw_black_pixel" here:

Anyway, it looks like you are having fun, I hope this helps! :)

By santiontanon

Paragon (1833)

santiontanon さんの画像

17-09-2021, 16:43

That being said, writing pixels one by one will be very slow. So, if you want to do a Conway's game of life that changes the whole screen, probably the best would be to have a buffer in memory with all the pixels, then update that in each cycle of the "game of life", and finally copy the whole thing at once to video memory.

By bverhage

Supporter (9)

bverhage さんの画像

17-09-2021, 18:14

Thank you for the examples! Looking into them to see if I can get my idea to work.

After some googling I already guessed it would be quicker to work with a buffer in memory and copy that to video memory. But since it's fun to play around I also want to try to write it pixel by pixel. Just because I had this in my mind. Hopefully it will give me a better understanding of what I am doing and whySmile

By Randam

Paragon (1431)

Randam さんの画像

17-09-2021, 19:19

That program has a game of life game on the disk. It is the second option. Visually nice too!!

I also know there was a game of life implementation on one of the MSX Fan's, at least as a listing. Perhaps also on the disks... I'd have to check if that is interesting...

By the way... reminded me that he died last year. Great mathematician...

By Amaury Carvalho

Resident (41)

Amaury Carvalho さんの画像

17-09-2021, 21:13

Here you can find a MSX Basic implementation of Conway's game of life in textual screen mode, that you can also compile using MSXBAS2ROM to speed it.


Enlighted (6098)


18-09-2021, 14:27

Yes, it is definitely good idea to keep the cells in RAM and only draw picture to VRAM.
It is possible to make it fairly fast... My personal suggestion is that you walk the cells in S-shape form like this:

This way you get 9-bit value (8bits + carry) that you can use to make a cell status fetch from 9-bit table. The nice thing in this method is that for each next cell on a row you only need to rotate in 3 new cells and do a table lookup.
You better to buffer the results also on RAM and then just write the VRAM 256 x 8-pixels at a time.