summaryrefslogtreecommitdiff
path: root/msim/sim.h
blob: e0ac4bb4c027433d5e2ad579a737affae636e309 (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
/* Copyright (c) 2024 Freya Murphy */

#ifndef __SIM_H__
#define __SIM_H__

#include <elf.h>
#include <stdint.h>

/// arguments
struct simulator_args {
	char *executable;
	bool jdelay;
	bool debug;
};


// when loading the executable, we need to bounds check to
// make sure its within the file size
//
// this checks that
// 1. the end is in the file
// 2. the off and len doesnt integer overflow
#define BOUND_CHK(size, len, off) \
	((off) > UINT32_MAX - (len) || (off) + (len) > (size))

///
/// data must start at these
/// addresses or the executable is
/// invalid
///
#define MEM_TEXT_ADDR 0x00400000
#define MEM_DATA_ADDR 0x10000000

struct simulator {
	struct simulator_args *args;

	/// the registers
	uint32_t reg[32];

	/// the program counter
	uint32_t pc;

	/// the current executing pc
	uint32_t current_pc;

	/// low and hi
	uint32_t low;
	uint32_t hi;

	/// memory address bounds
	uint32_t data_min;
	uint32_t data_max;
	uint32_t text_min;
	uint32_t text_max;

	// entrypoint
	uint32_t entry;
};

/* initalize a simulator */
int sim_init(struct simulator *sim, struct simulator_args *args);

/* load a file into a simulator */
int sim_load_file(struct simulator *sim);

/* execute and instruction */
void sim_ins(struct simulator *sim, uint32_t ins);

/* execute the next instruction in the simulator */
void sim_step(struct simulator *sim);

/* run the simulator with the loaded state */
_Noreturn void sim_run(struct simulator *sim);

/* run the debugger for a simulator */
_Noreturn void sim_debug(struct simulator *sim);

/* panics the simulator */
__attribute__((format(printf, 2, 3)))
_Noreturn void sim_dump(struct simulator *sim, const char *format, ...);

/* dumps registers */
void sim_dump_reg(struct simulator *sim);

#endif /* __SIM_H__ */