Not sure how to follow drawPixel algorithim

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Not sure how to follow drawPixel algorithim

fishboy1887
I have tried following the drawPixel algorithim from figure 12.6 in TECS edition 2 but have clearly gone wrong somewhere since there is no ouput when I tried testing drawPixel just by calling it in Main.jack.

    function void drawPixel(int x, int y) {
        var int pixelLocation, q, p, m, word, bitmask, i;
        let pixelLocation = 16384 + y * 32 + x / 16;
        // Since Jack has no % operator, we implement it here instead
        let q = x / 16;
        let p = q * 16;
        let m = x - p;

        // Bitmask with only m-th bit set to 1, realize << bitshift operator
        let bitmask = 1;
        let i = 0;
        while (i < m) {
            let bitmask = bitmask * 2;
            let i = i + 1;
        }

        let word = Memory.peek(pixelLocation);

        // Set or clear the bit based on the current color
        if (currentColour) {
            let word = word | bitmask;
        } else {
            let word = word & (~bitmask);
        }

        do Memory.poke(pixelLocation, word);
        return;
    }

Here is the algorithim:

drawPixel(x, y):
    Using x and y, computer RAM address where the pixel is represented;
 
    Using Memory.peek, get the 16-bit value of this address;
 
    Using some bitwise operation, set (only) the bit that corresponds to the pixel to the current color;

    Using Memory.poke, write the modified 16-bit value "back" to the RAM address
Reply | Threaded
Open this post in threaded view
|

Re: Not sure how to follow drawPixel algorithim

dolomiti7
This doesn't work for the same reason why your space invaders game doesn't work (I gave you a hint there). The calculation of the pixel location will go wrong. Add in a debug statement there to print out the calculated address and you will see.
Reply | Threaded
Open this post in threaded view
|

Re: Not sure how to follow drawPixel algorithim

fishboy1887
Cheers!