drawing screen pixels and output characters

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

drawing screen pixels and output characters

cipherdog
This class has been such an amazing learning experience so far. Now, I've been on the OS for weeks, it's excruciating. Reading this forum has helped a ton... so I thought posting might help too! My current sticking point I'm getting stuck implementing the drawing of pixels and also have a question about output.printChar

I'm able to do the math to find the proper word to edit, and I understand that I can perform a bitmask operation to toggle the appropriate bit. Here are a few ideas I have and where I'm struggling:

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

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?

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.

any help or hints would be very appreciated

thank you
Reply | Threaded
Open this post in threaded view
|

Re: drawing screen pixels and output characters

cadet1620
Administrator
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
Reply | Threaded
Open this post in threaded view
|

Re: drawing screen pixels and output characters

cipherdog
Thank you very much Mark. You gave me a lot of food for thought and I've been working on various problems with my OS implementation. the bitmask examples helped me a lot. I also had an epiphany about how to handle modular arithmetic at least for powers of 2. it's starting to sink in!