|
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
|