#### MemoryDiag is both a pass/fail test and a diagnostic.

MemoryDiag tests the following:

1. Memory.peek() and Memory.poke() read from and write to the specified memory address.
2. Memory.alloc() returns RAM blocks that are fully contained within the heap address range 2048-16383.
3. Memory.alloc() does not return RAM blocks that overlap each other.
4. RAM blocks deallocated by Memory.deAlloc() are made available for Memory.alloc() to reuse.

The block reuse test allocates and deallocates an 8000 word block. It then tries to allocates a 7000 word block which must be allocated from the deallocated 8000 word block. If the 8000 word block is not available for reuse, there will only be about 6300 words available in the heap so you will get an ERR6.

At the end of this test it is normal to see some pixels set on the screen. This is because the results of the test are written to RAM[17000] – RAM[17008] which is in the Screen memory. MemoryDiag does not put its results in the first 16K of RAM because it must not interfere with the Memory.jack that is being tested.

#### Using MemoryDiag as a diagnostic

RAM[17000] is set to a unique value before every system call and address validation. This allows the exact failure location in the test to be identified when automated testing is used. At the end of the test, RAM[17000] is set to 100.

When the test fails to compare, look at the MemoryDiag.out file and note the RAM[17000] value. This is the test step that failed. Look through the Main.jack code and find the corresponding
`let out[0] = step;`
statement. The function immediately following this statement is where the failure occurred.

For example, if RAM[17000] is 51, the
`do Memory.deAlloc(b);`
call did not return. Either there was a simulation error like a bad address or deAlloc() got stuck in a loop.

#### Sample MemoryDiag output files

Note that RAM[17003] – RAM[17008] are "don't care" values in the MemoryDiag.cmp file.

Supplied Memory.vm passes:

    |RAM[17000|RAM[17001|RAM[17002|RAM[17003|RAM[17004|RAM[17005|RAM[17006|RAM[17007|RAM[17008|
    |     100 |     333 |     334 |    2050 |    2072 |    2077 |    2050 |    2050 |    2050 |

Memory.Jack using the Coursera implementation passes:

    |RAM[17000|RAM[17001|RAM[17002|RAM[17003|RAM[17004|RAM[17005|RAM[17006|RAM[17007|RAM[17008|
    |     100 |     333 |     334 |   16364 |   16359 |   15857 |   15852 |    7850 |    8850 |

Broken Memory.jack fails (alloc() returns duplicate block address):

    |RAM[17000|RAM[17001|RAM[17002|RAM[17003|RAM[17004|RAM[17005|RAM[17006|RAM[17007|RAM[17008|
    |      32 |     333 |     334 |    2050 |    2050 |       0 |       0 |       0 |       0 |

Broken Memory.jack fails (deAlloc() does not recycle memory blocks):

    |RAM[17000|RAM[17001|RAM[17002|RAM[17003|RAM[17004|RAM[17005|RAM[17006|RAM[17007|RAM[17008|
    |      73 |     333 |     334 |   16364 |   16359 |   15857 |   15852 |    7850 |       0 |