cipherdog wrote
1. Did you implement an array in the Screen class similar to two_to_the in the Math class in order to be able to call the bit masking operations? This seems wasteful since two_to_the is already allocated. Is there any way on the Jack/Hack setup to declare a global variable?
2. Following from that, it would make sense potentially to use the services of Math.bit(). But it's not a built-in function so I'm not sure if that's considered 'kosher?'
Yes, I had the power-of-two array in both Math and Screen. I wanted to keep the classes interchangeable with the supplied OS classes to make it easier to debug by developing each class independently.
Using Math.bit() will be slow due to the high overhead of function calls. You could share the array by adding a function to Math.class to return the Array pointer.
class Screen {
static Array bit;
...
function void init() {
bit = Math.pow2array();
...
Then you can just use bit[n] in Screen.
Math.init() must already be called before Screen.init() since Screen functions use multiplication and division.
3. I had the same idea, to implement Math.modulo and use that other places. But, I decided against it because again I am trying to stick to the specification given in the book. As an aside, when i needed to calculate the result mod n of a number i made a call to Math.multiply and Math.divide. Is there a better/different way of doing it?
It's your OS. You can add extensions to it as you like. You could also add the '%' operator to your Jack compiler.
4. It seems wasteful to call Screen.drawPixel() in Output.printChar() since blocks of 8 bits could easily be toggled at a time. The part of this that I'm struggling with is conceptually how to create the appropriate bit mask. So when the cursor_column is odd, I need to manipulate some bits in the screen word, and if it's even, I need to manipulate the other half of the bits. I'm not sure what kinds of schemes I might want to use for this.
Shifting the font characters whenever you write a character to the high half of the screen word is expensive. You can do the shifting once when you create the font table.
Class Output {
function void create(...) {
let map = Array.new(11);
let charMaps[index] = map;
let map[0] = a | (a*256);
...
When you determine if you are drawing a character to an odd or even column, you can set fontMask and screenMask to appropriate bit masks (255 or ~255) so that the character drawing loop can simply do
screen[i] = (screen[row+i] & screenMask) | (map[i] & fontMask);
--Mark