-
Notifications
You must be signed in to change notification settings - Fork 3
/
bp_utils.c
87 lines (71 loc) · 1.88 KB
/
bp_utils.c
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
#include <stdint.h>
#include "bp_utils.h"
// BAREMETAL ifdef overrides the weak dramfs_init function such that we don't
// need to go through filesystem initialization if we're not using this
// feature.
#ifdef BAREMETAL
int dramfs_fs_init() { return 0; };
#endif
uint64_t bp_get_hart() {
uint64_t core_id;
__asm__ volatile("csrr %0, mhartid": "=r"(core_id): :);
return core_id;
}
void bp_barrier_end(volatile uint64_t *barrier_address, uint64_t total_num_cores) {
uint64_t atomic_inc = 1;
uint64_t atomic_result;
__asm__ volatile("amoadd.d %0, %2, (%1)": "=r"(atomic_result)
: "r"(barrier_address), "r"(atomic_inc)
:);
while(*barrier_address < total_num_cores);
bp_finish(0);
}
void bp_finish(uint8_t code) {
uint64_t core_id = bp_get_hart();
*(FINISH_BASE_ADDR+core_id*8) = code;
__asm__ volatile("fence");
while (1);
}
void bp_hprint(uint8_t nibble) {
if (nibble <= 0x9) {
bp_cprint('0' + nibble);
} else if (nibble <= 0xf) {
bp_cprint('a' + nibble - 0xa);
}
}
void bp_cprint(uint8_t ch) {
*(PUTCHAR_BASE_ADDR) = ch;
}
void bp_hprint_uint64(uint64_t val) {
bp_cprint('0');
bp_cprint('x');
for (int i = 0; i < 16; i++) {
uint8_t nibble = val >> 60;
bp_hprint(nibble);
val <<= 4;
}
}
void bp_print_string(char *str) {
while (*str) {
bp_cprint(*str);
str++;
}
}
int bp_cget(void) {
char c = *(GETCHAR_BASE_ADDR);
// We don't have a empty fifo signal so just alias 255 to -1 for now, to simulate EOF
if (c == 255) return -1;
// Substitute CR with newline to make scanf work
if (c == '\r') return '\n';
return c;
}
void bp_panic(char *message) {
if (message) {
bp_print_string(message);
}
bp_finish(1);
while (1);
}
uint32_t bp_param_get(char *addr) {
return *(volatile uint32_t *) addr;
}