summaryrefslogtreecommitdiff
path: root/memory/cache.h
blob: 85549106d43855c25695eec7291897e4e3b81f44 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once

#include <array>
#include <cstdint>

#include "memory/dram.h"
#include "memory/line.h"

namespace memory {
    template<unsigned int SETS_LOG2, unsigned int WAYS> struct inline_cache {
        static constexpr std::uint64_t SETS = 1 << SETS_LOG2;
        static constexpr std::uint64_t LINE_SET_OFFSET_MASK = SETS - 1;

        std::uint64_t last_serial = 0;

        struct tag {
            std::uint64_t serial = 0;
            std::uint64_t line_address;
            infra::transaction transaction;
        };

        typedef std::array<tag, WAYS> set_tags;
        typedef std::array<line, WAYS> set_data;

        std::array<set_tags, SETS> tags;
        std::array<set_data, SETS> data;

        void handle_response(const dram::response &r) {
            auto set_address = r.line_address & LINE_SET_OFFSET_MASK;
            auto &stags = tags[set_address];
            auto &sdata = data[set_address];
            for (unsigned int i = 0; i < WAYS; ++i) {
                auto &stag = stags[i];
                if (stag.serial && stag.line_address == r.line_address) {
                    handle_response(r, stag, sdata[i]);
                    return;
                }
            }
            std::uint64_t min_serial = ~(std::uint64_t)0;
            unsigned int victim;
            for (unsigned int i = 0; i < WAYS; ++i) {
                auto &stag = stags[i];
                if (stag.serial < min_serial) {
                    min_serial = stag.serial;
                    victim = i;
                }
            }
            handle_response(r, stags[victim], sdata[victim]);
        }

        void handle_response(const dram::response &r, tag &t, line &d) {
            t.serial = ++last_serial;
            t.line_address = r.line_address;
            t.transaction = r.transaction;
            d = r.data;
        }

        std::optional<tag> probe(std::uint64_t address) {
            auto line_address = address >> LINE_BYTES_LOG2;
            auto set_address = line_address & LINE_SET_OFFSET_MASK;
            auto &stags = tags[set_address];
            for (unsigned int i = 0; i < WAYS; ++i) {
                auto &stag = stags[i];
                if (stag.serial && stag.line_address == line_address)
                    return stag;
            }
            return {};
        }

        std::optional<tag> fetchline(line &dst, std::uint64_t address) {
            auto line_address = address >> LINE_BYTES_LOG2;
            auto set_address = line_address & LINE_SET_OFFSET_MASK;
            auto &stags = tags[set_address];
            auto &sdata = data[set_address];
            for (unsigned int i = 0; i < WAYS; ++i) {
                auto &stag = stags[i];
                if (stag.serial && stag.line_address == line_address) {
                    stag.serial = ++last_serial;
                    dst = sdata[i];
                    return stag;
                }
            }
            return {};
        }

        std::optional<tag> fetch(unsigned int &dst, std::uint64_t address) {
            line data;
            if (auto tag = fetchline(data, address); tag.has_value()) {
                auto line_offset = address & LINE_BYTE_OFFSET_MASK;
                dst = data[line_offset];
                return tag;
            }
            return {};
        }
    };
}