summaryrefslogtreecommitdiff
path: root/main.c
blob: a136a40dfab812a1c72ed3b6686d0c318076d2ea (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
/* Copyright (C) 2024 Aryadev Chavali

 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License Version 2 for
 * details.

 * You may distribute and modify this code under the terms of the GNU General
 * Public License Version 2, which you should have received a copy of along with
 * this program.  If not, please go to <https://www.gnu.org/licenses/>.

 * Created: 2024-10-25
 * Author: Aryadev Chavali
 * Description: Entrypoint
 */

#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

typedef struct
{
  u8 *buffer;
  u64 size_memory, size_stack, size_registers;
} vm_t;

#define VM_DEFAULT_MEM_SIZE       (1024)
#define VM_DEFAULT_STACK_SIZE     (128 * sizeof(u64))
#define VM_DEFAULT_REGISTERS_SIZE (8 * sizeof(u64))

#define VM_STACK(VM)     ((VM)->buffer + (VM)->size_memory + (VM)->size_registers)
#define VM_REGISTERS(VM) ((u64 *)((VM)->buffer + (VM)->size_memory))
#define VM_IP(VM)        (VM_REGISTERS(VM)[0])
#define VM_SP(VM)        (VM_REGISTERS(VM)[1])

void vm_init_malloc(vm_t *vm)
{
  if (vm->size_stack == 0)
    vm->size_stack = VM_DEFAULT_STACK_SIZE;
  if (vm->size_registers == 0)
    vm->size_registers = VM_DEFAULT_REGISTERS_SIZE;
  if (vm->size_memory == 0)
    vm->size_memory = VM_DEFAULT_MEM_SIZE;

  vm->buffer = calloc(1, vm->size_memory + vm->size_stack + vm->size_registers);
}

void vm_stop(vm_t *vm)
{
  free(vm->buffer);
  memset(vm, 0, sizeof(*vm));
}

int main(void)
{
  vm_t vm = {0};
  vm_init_malloc(&vm);
  vm_stop(&vm);
  return 0;
}