test/vector_machine.c 1.8 KiB raw
1
/* Integer vector execution through the machine instruction dispatcher. */
2
#include <assert.h>
3
#include <string.h>
4
5
#include "machine_internal.h"
6
7
/* Write one instruction to machine RAM. */
8
static void instruction(u8 *ram, u32 ins) {
9
    for (u32 i = 0; i < 4; i++)
10
        ram[i] = ins >> (8 * i);
11
}
12
13
int main(void) {
14
    u8                    ram[4096] = { 0 };
15
    struct machine_config config    = { 0 };
16
    config.ram                      = ram;
17
    config.memory_size              = sizeof(ram);
18
    config.hart_count               = 1;
19
    config.text.base                = MACHINE_RAM_BASE;
20
    config.text.size                = 64;
21
    struct machine machine          = { 0 };
22
    machine.config                  = &config;
23
    struct machine_hart *h          = &machine.harts[0];
24
    h->privilege                    = 3;
25
    h->pc                           = MACHINE_RAM_BASE;
26
    instruction(ram, 0xc1027557); /* vsetivli a0,4,e32,m1,tu,mu */
27
    machine_step(&machine, h);
28
    assert(h->mcause == CAUSE_ILLEGAL && h->vector.vl == 0);
29
    h->mstatus = 1ULL << 9;
30
    h->pc      = MACHINE_RAM_BASE;
31
    machine_step(&machine, h);
32
    assert(h->vector.vl == 4 && h->regs[10] == 4);
33
    assert(((h->mstatus >> 9) & 3) == 3);
34
    instruction(ram, 0x5e03b457); /* vmv.v.i v8,7 */
35
    h->pc = MACHINE_RAM_BASE;
36
    machine_step(&machine, h);
37
    assert(h->vector.reg[8][0] == 7);
38
    instruction(ram, 0x42802557); /* vmv.x.s a0,v8 */
39
    h->pc = MACHINE_RAM_BASE;
40
    machine_step(&machine, h);
41
    assert(h->regs[10] == 7);
42
    instruction(ram, 0x02056407); /* vle32.v v8,(a0) */
43
    h->regs[10] = MACHINE_RAM_BASE + 4092;
44
    h->pc       = MACHINE_RAM_BASE;
45
    machine_step(&machine, h);
46
    assert(h->mcause == CAUSE_LOAD_FAULT && h->vector.vstart == 1);
47
    assert(h->mtval == MACHINE_RAM_BASE + 4096);
48
    return 0;
49
}