summaryrefslogtreecommitdiff
path: root/src/graphics.c
blob: 8114f30057bd756d28660fc7a182a24bf1180c9e (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Project: Salis
// Author:  Paul Oliver
// Email:   contact@pauloliver.dev

/*
 * This module renders the contents of the VM memory buffer into a 7 channel
 * image. It supports zooming in and out, condensing the state of several
 * bytes of memory into single pixels, when zoomed out. When zoomed in, each
 * pixel represents a single byte in memory.
 */

u64 g_gfx_vsiz;     // zoom level

u64 *g_gfx_inst;    // instruction channel
u64 *g_gfx_mall;    // allocated state channel
u64 *g_gfx_mbst;    // memory block start channel
u64 *g_gfx_mb0s;    // selected organism's memory block #1 channel
u64 *g_gfx_mb1s;    // selected organism's memory block #2 channel
u64 *g_gfx_ipas;    // selected organism's IP channel
u64 *g_gfx_spas;    // selected organism's SP channel

void gfx_init(u64 vsiz) {
    assert(vsiz);

    g_gfx_vsiz = vsiz;

    g_gfx_inst = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_mall = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_mbst = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_mb0s = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_mb1s = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_ipas = calloc(g_gfx_vsiz, sizeof(u64));
    g_gfx_spas = calloc(g_gfx_vsiz, sizeof(u64));

    assert(g_gfx_inst);
    assert(g_gfx_mall);
    assert(g_gfx_mbst);
    assert(g_gfx_mb0s);
    assert(g_gfx_mb1s);
    assert(g_gfx_ipas);
    assert(g_gfx_spas);
}

void gfx_free() {
    if (g_gfx_vsiz == 0) {
        return;
    }

    assert(g_gfx_inst);
    assert(g_gfx_mall);
    assert(g_gfx_mbst);
    assert(g_gfx_mb0s);
    assert(g_gfx_mb1s);
    assert(g_gfx_ipas);
    assert(g_gfx_spas);

    g_gfx_vsiz = 0;

    free(g_gfx_inst);
    free(g_gfx_mall);
    free(g_gfx_mbst);
    free(g_gfx_mb0s);
    free(g_gfx_mb1s);
    free(g_gfx_ipas);
    free(g_gfx_spas);

    g_gfx_inst = NULL;
    g_gfx_mall = NULL;
    g_gfx_mbst = NULL;
    g_gfx_mb0s = NULL;
    g_gfx_mb1s = NULL;
    g_gfx_ipas = NULL;
    g_gfx_spas = NULL;
}

void gfx_resize(u64 vsiz) {
    assert(vsiz);

    gfx_free();
    gfx_init(vsiz);
}

void gfx_render_inst(const Core *core, u64 pos, u64 zoom) {
    assert(core);

    for (u64 i = 0; i < g_gfx_vsiz; ++i) {
        g_gfx_inst[i] = 0;
        g_gfx_mall[i] = 0;

        for (u64 j = 0; j < zoom; ++j) {
            u64 addr = pos + (i * zoom) + j;
            u8  byte = mvec_get_byte(core, addr);

            g_gfx_inst[i] += byte;
            g_gfx_mall[i] += (byte & MALL_FLAG) ? 1 : 0;
        }
    }
}

void gfx_clear_array(u64 *arry) {
    assert(arry);
    memset(arry, 0, g_gfx_vsiz * sizeof(u64));
}

void gfx_accumulate_pixel(u64 pos, u64 zoom, u64 pixa, u64 *arry) {
    assert(arry);

    u64 beg_mod = pos % MVEC_SIZE;
    u64 end_mod = beg_mod + (g_gfx_vsiz * zoom);
    u64 pix_mod = pixa % MVEC_SIZE;

#ifndef NDEBUG
    u64 inc_cnt = 0;
#endif

    while (pix_mod < end_mod) {
        if (pix_mod >= beg_mod && pix_mod < end_mod) {
            u64 pixi = (pix_mod - beg_mod) / zoom;
            assert(pixi < g_gfx_vsiz);
            arry[pixi]++;

#ifndef NDEBUG
            inc_cnt++;
#endif
        }

        pix_mod += MVEC_SIZE;
    }


#ifndef NDEBUG
    if (zoom != 1) {
        assert(inc_cnt <= 2);
    }
#endif
}

void gfx_render_mbst(const Core *core, u64 pos, u64 zoom) {
    assert(core);

    gfx_clear_array(g_gfx_mbst);

    for (u64 pix = core->pfst; pix <= core->plst; ++pix) {
        u64 mb0a = arch_proc_mb0_addr(core, pix);
        u64 mb1a = arch_proc_mb1_addr(core, pix);

        gfx_accumulate_pixel(pos, zoom, mb0a, g_gfx_mbst);
        gfx_accumulate_pixel(pos, zoom, mb1a, g_gfx_mbst);
    }
}

void gfx_render_mb0s(const Core *core, u64 pos, u64 zoom, u64 psel) {
    assert(core);

    gfx_clear_array(g_gfx_mb0s);

    if (psel < core->pfst || psel > core->plst) {
        return;
    }

    u64 mb0a = arch_proc_mb0_addr(core, psel);
    u64 mb0s = arch_proc_mb0_size(core, psel);

    for (u64 i = 0; i < mb0s; ++i) {
        gfx_accumulate_pixel(pos, zoom, mb0a + i, g_gfx_mb0s);
    }
}

void gfx_render_mb1s(const Core *core, u64 pos, u64 zoom, u64 psel) {
    assert(core);

    gfx_clear_array(g_gfx_mb1s);

    if (psel < core->pfst || psel > core->plst) {
        return;
    }

    u64 mb1a = arch_proc_mb1_addr(core, psel);
    u64 mb1s = arch_proc_mb1_size(core, psel);

    for (u64 i = 0; i < mb1s; ++i) {
        gfx_accumulate_pixel(pos, zoom, mb1a + i, g_gfx_mb1s);
    }
}

void gfx_render_ipas(const Core *core, u64 pos, u64 zoom, u64 psel) {
    assert(core);

    gfx_clear_array(g_gfx_ipas);

    if (psel < core->pfst || psel > core->plst) {
        return;
    }

    u64 ipa = arch_proc_ip_addr(core, psel);

    gfx_accumulate_pixel(pos, zoom, ipa, g_gfx_ipas);
}

void gfx_render_spas(const Core *core, u64 pos, u64 zoom, u64 psel) {
    assert(core);

    gfx_clear_array(g_gfx_spas);

    if (psel < core->pfst || psel > core->plst) {
        return;
    }

    u64 spa = arch_proc_sp_addr(core, psel);

    gfx_accumulate_pixel(pos, zoom, spa, g_gfx_spas);
}

void gfx_render(const Core *core, u64 pos, u64 zoom, u64 psel) {
    assert(core);

    gfx_render_inst(core, pos, zoom);
    gfx_render_mbst(core, pos, zoom);
    gfx_render_mb0s(core, pos, zoom, psel);
    gfx_render_mb1s(core, pos, zoom, psel);
    gfx_render_ipas(core, pos, zoom, psel);
    gfx_render_spas(core, pos, zoom, psel);
}