Built in function trried to access memory outside heap or screen

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

Built in function trried to access memory outside heap or screen

sudhabindu1995
I have completed all the OS classes and passed all the individual tests. But the following problem arises whenever I try to run them on Pong:
In my Pong directory, if I put Math.jack and Sys.jack together, I get the error Built in function trried to access memory outside heap or screen
However, if I put only one of them (plus the other 6 class files), it works just fine
Reply | Threaded
Open this post in threaded view
|

Re: Built in function trried to access memory outside heap or screen

cadet1620
Administrator
These sorts of interaction problems can be hard to debug.

First, some of the OS files' init() functions use other OS classes. For instance, Math.init() calls Array.new() to allocate the static constant arrays. Array.new() calls Memory.alloc(). This means that Memory.init() must be called before Math.init().

There are other dependencies as well. The supplied Sys.vm calls the other init() functions in this order:
    Memory.init
    Math.init
    Screen.init
    Output.init
    Keyboard.init

The best way to debug this is probably to start copying your OS VM files one-by-one into ScreenTest, starting with your Sys.vm, then Math.vm, then Array.vm, then Memory.vm. (Keyboard, Output and String are not used in ScreenTest.)

Then you can do the same thing in OutputTest then StringTest. Once you get all the Tests running with all your OS vm files, try Pong again.


If you implemented Math.divide() using the recursive algorithm in the book, you might find that it sometimes returns the wrong answer or stack overflows. There is a subtle problem with implementing this algorithm in a fixed word size computer. The q = divide(x, 2y) step fails if 2y > 32767 since that 2y value cannot be represented in 16-bit signed integers.

The fix is to change the call to:

    if (y < 16384)
        q = divide(x, 2y)
    else
        q = 0

--Mark


Reply | Threaded
Open this post in threaded view
|

Re: Built in function trried to access memory outside heap or screen

sudhabindu1995
Thanks, Ordering the init calls resolved the issue.
As written in the concluding sentence of the book, I am now a proud owner of an operating System written by me

P.S: The Pong game is super slow though. I know it's obvious with so many high level calls being involved.
But it is ridiculously slow after adding my Sys.vm file. Any remarks
Reply | Threaded
Open this post in threaded view
|

Re: Built in function trried to access memory outside heap or screen

cadet1620
Administrator
All of Pong's graphics are done with Screen.drawRectangle() calls.

If your Screen class is typical, you have a drawHorixontalLine() that handles the drawLine() case when x1==x2, and your drawRectangle() calls that to draw the required lines to fill the rectangle.

drawHorizontalLine() is probably a while loop that calls drawPixel() for each of the x2-x1 pixels. Each drawPixel() does multiplication and division to locate the pixel.

You can speed up drawHorizontalLine() by computing the addresses of the first and last words in the line that contain pixels in the line and bitmasks for the first and last words. All the interior words have all their bits set in a single operation by setting the word to either 0 or -1.

Example: drawLine(76, 32, 131, 32) drawing black
17000: or -4096   1111000000000000
17001: set   -1   1111111111111111
17002: set   -1   1111111111111111
17003: set   -1   1111111111111111
17004: or  1023   0000001111111111

Building the bitmasks is a bit expensive. This is a good application for another static array.
    rightMask[n], n=0..16
returns a word with the 'n' right-most bits set.

--Mark