summaryrefslogtreecommitdiff
path: root/io/model.cpp
blob: fb9f62b2b60965faecd4b00c1f2ebb69b272044e (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
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <utility>

#include "io/model.h"
#include "isa/isa.h"

bool iomodel::interact(std::array<std::uint_fast32_t, NUM_CTLREGS> &ctlregs) {
    auto [ebegin, eend] = log.equal_range(time);
    for (auto e = ebegin; e != eend; ++e) {
        auto &r = ctlregs[e->second.reg];
        r &= ~e->second.mask;
        r |= e->second.value;
    }

    ++time;

    if (ctlregs[TT_BITS] & TTO_TX) {
        std::cout << (char)(((ctlregs[TT_BITS] & TTO_DATA) >> TTO_DATA_SHIFT) & 0x7f) << std::flush;
        ctlregs[TT_BITS] &= ~TTO_TX & ~TTO_DATA;
        log.emplace(time + TT_OUTPUT_DELAY, event(TT_BITS, TTO_FLAG, 0));
    }

    bool interrupt = false;
    if (ctlregs[TT_FLAGS] & TTF_INT_ENABLE) {
        if (ctlregs[TT_BITS] & TTI_FLAG)
            interrupt = true;
        if (ctlregs[TT_BITS] & TTO_FLAG)
            interrupt = true;
    }
    if (interrupt)
        ctlregs[FLAGS] |= FLAG_INT_REQUEST;
    else
        ctlregs[FLAGS] &= !FLAG_INT_REQUEST;
    interrupt = interrupt && (ctlregs[FLAGS] & FLAG_INT_ENABLE);

    if (interrupt) {
        ctlregs[FLAGS_SAVED] = ctlregs[FLAGS];
        ctlregs[FLAGS] = 0;
        ctlregs[HALTED] = 0;
    } else {
        if (ctlregs[FLAGS] & FLAG_INT_ENABLE_DELAY)
            ctlregs[FLAGS] |= FLAG_INT_ENABLE;
    }

    return interrupt;
}

void iomodel::load_evt(std::istream &fh) {
    // File format:
    //  +time                   advances time by relative increment
    //  regname=value/mask      adds event that sets regname = (regname & ~mask) | value
    //  regname=value           adds event that sets regname = value
    // FIXME: add assertions
    // FIXME: add DMA
    for (std::string line; std::getline(fh, line); ) {
        if (line.size() == 0)
            continue;
        if (line[0] == '+') {
            char *end = nullptr;
            auto step = std::strtoull(line.c_str(), &end, 0);
            assert(end && *end == '\0');
            load_time += step;
        } else {
            auto eqpos = line.find('=');
            assert(eqpos != std::string::npos);
            auto regname = line.substr(0, eqpos);
            ctlreg reg = ctlreg_map.at(regname);
            std::uint_fast32_t value, mask;
            auto slashpos = line.find('/', eqpos);
            auto valuestr = line.substr(eqpos + 1, slashpos - (eqpos + 1));
            char *end = nullptr;
            value = std::strtoull(valuestr.c_str(), &end, 0);
            assert(end && *end == '\0');
            if (slashpos == std::string::npos) {
                mask = ~(std::uint_fast32_t)0;
            } else {
                auto maskstr = line.substr(slashpos + 1);
                mask = std::strtoull(maskstr.c_str(), &end, 0);
                assert(end && *end == '\0');
            }
            log.emplace(load_time, event(reg, value, mask));
        }
    }
}