Memory Module Specification

Read time: 4 minutes (1154 words)

Here is a formal specification of the memory unit for our system. Do not be frightened by this notation, it is a mathematical form designed to be concise and correct. You can use notatiation and mathematical techniques to analyze the system. We will not explore that kind of design work here, it is the stuff of a graduate course!

Even if you never plan on learning about thiskind of stuff, this can help in your design. Math, in general, tries to eliminate the sources of confusion introduced when humans write down specifications. Formal methods, based on predicate calculus, which you will learn in a discrete mathematicecourse as you get further along, is sound enough to apply mathematical proof techniques to prove that your system will work as planned.

This kind of thing is hard to do, but pretty cool. The British Air Trafic Cntrol system was partly resesigned using formal methods, and they have not found an error in the part of the system where they sed these techniques. Try that, Microsoft!

Testing Memory

We should be thinking about how the memory unit is to work, before we ever write aline of code. Here is a catch.hpp test that will check the unit tout.

tests/test_memory.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "catch.hpp"

TEST_CASE("test memory constructor", "memory") {
    const int SIZE = 10;
    Memory mem(SIZE);
    REQUIRE(mem.get_size() == SIZE);
}

TEST_CASE("test memory read", "memory") {
    const int SIZE = 10;
    Memory mem(SIZE);
    mem.set_MADDR(2);
    mem.tick();
    REQUIRE(mem.get_DATA() == 42);
}

TEST_CASE("test memory write", "memory") {
    const int SIZE = 10;
    Memory mem(SIZE);
    // write data
    mem.set_MADDR(2);
    mem.set_DATA(42);
    mem.tick();
    // verify write worked
    REQUIRE(mem.get_DATA() == 42);
}