summaryrefslogtreecommitdiff
path: root/isa/checker.h
blob: d70997fab8513a1c9bf360c4077eb43737a83ef0 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once

#include <array>
#include <map>

#include "isa/isa.h"

struct funcmem {
    static constexpr unsigned int PAGE_BYTES_LOG2 = 20;
    static constexpr unsigned int PAGE_BYTES = 1 << PAGE_BYTES_LOG2;
    static constexpr unsigned int PAGE_BYTE_OFFSET_MASK = PAGE_BYTES - 1;

    typedef std::array<unsigned int, PAGE_BYTES> page;

    std::map<unsigned int, page> image;

    unsigned int fetch(unsigned int address) const {
        auto page_address = address >> PAGE_BYTES_LOG2;
        auto page_offset = address & PAGE_BYTE_OFFSET_MASK;
        if (auto p = image.find(page_address); p != image.end()) {
            const auto &page = p->second;
            return page[page_offset];
        }
        return 0;
    }

    void store(unsigned int address, unsigned int value) {
        auto page_address = address >> PAGE_BYTES_LOG2;
        auto page_offset = address & PAGE_BYTE_OFFSET_MASK;
        auto [p, emplaced] = image.try_emplace(page_address);
        auto &page = p->second;
        if (emplaced)
            page.fill(0);
        page[page_offset] = value;
    }
};

struct checker {
    unsigned int acc = 0;
    unsigned int link = 0;
    unsigned int mq = 0;
    unsigned int pc = 00200;
    std::array<unsigned int, NUM_CTLREGS> ctlregs;
    instruction_context inst;
    bool halted = false;
    funcmem mem;
    checker()
    {
        ctlregs.fill(0);
    }
    void execute();
};