-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathio.h
63 lines (48 loc) · 1.62 KB
/
io.h
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
/*
* rv32emu is freely redistributable under the MIT License. See the file
* "LICENSE" for information on usage and redistribution of this file.
*/
#pragma once
#include <stdint.h>
#include <string.h>
/* main memory */
typedef struct {
uint8_t *mem_base;
uint64_t mem_size;
} memory_t;
/* create a memory instance */
memory_t *memory_new(uint32_t size);
/* delete a memory instance */
void memory_delete(memory_t *m);
/* read an instruction from memory */
uint32_t memory_ifetch(uint32_t addr);
/* read a word from memory */
uint32_t memory_read_w(uint32_t addr);
/* read a short from memory */
uint16_t memory_read_s(uint32_t addr);
/* read a byte from memory */
uint8_t memory_read_b(uint32_t addr);
/* read a length of data from memory */
void memory_read(const memory_t *m, uint8_t *dst, uint32_t addr, uint32_t size);
/* write a length of data to memory */
static inline void memory_write(memory_t *m,
uint32_t addr,
const uint8_t *src,
uint32_t size)
{
memcpy(m->mem_base + addr, src, size);
}
/* write a word to memory */
void memory_write_w(uint32_t addr, const uint8_t *src);
/* write a short to memory */
void memory_write_s(uint32_t addr, const uint8_t *src);
/* write a byte to memory */
void memory_write_b(uint32_t addr, const uint8_t *src);
/* write a length of certain value to memory */
static inline void memory_fill(memory_t *m,
uint32_t addr,
uint32_t size,
uint8_t val)
{
memset(m->mem_base + addr, val, size);
}