~vdupras/collapseos

ref: 6b895d07456e5776c1b3506e1ba46b9595cb5ffb collapseos/cvm/stage.c -rw-r--r-- 1.5 KiB
6b895d07Virgil Dupras sms: explicitly set ourselves in IM 1 11 months ago
                                                                                
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
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "vm.h"

#ifndef BLKFS_PATH
#error BLKFS_PATH needed
#endif
#define RAMSTART 0
#define STDIO_PORT 0x00
// To know which part of RAM to dump, we listen to port 2, which at the end of
// its compilation process, spits its HERE addr to port 2 (MSB first)
#define HERE_PORT 0x02

VM *vm;
// We support double-pokes, that is, a first poke to tell where to start the
// dump and a second one to tell where to stop. If there is only one poke, it's
// then ending HERE and we start at sizeof(KERNEL).
static uint16_t start_here = 0;
static uint16_t end_here = 0;

static uint8_t iord_stdio()
{
    int c = getc(stdin);
    if (c == EOF) {
        vm->running = false;
    }
    return (uint8_t)c;
}

static void iowr_stdio(uint8_t val)
{
    // comment if you don't like verbose staging output
    putc(val, stderr);
}

static void iowr_here(uint8_t val)
{
    start_here <<=8;
    start_here |= (end_here >> 8);
    end_here <<= 8;
    end_here |= val;
}

int main(int argc, char *argv[])
{
    if (argc < 2) {
        vm = VM_init(BLKFS_PATH);
    } else {
        vm = VM_init(argv[1]);
    }
    if (vm == NULL) {
        return 1;
    }
    vm->iord[STDIO_PORT] = iord_stdio;
    vm->iowr[STDIO_PORT] = iowr_stdio;
    vm->iowr[HERE_PORT] = iowr_here;
    while (VM_steps(1));

    // We're done, now let's spit dict data
    for (int i=start_here; i<end_here; i++) {
        putchar(vm->mem[i]);
    }
    VM_printdbg();
    VM_deinit();
    return 0;
}