1 /*******************************************************************************
2  * Copyright 2019-2021 Microchip FPGA Embedded Systems Solutions.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * MPFS HAL Embedded Software
7  *
8  */
9 
10 /***************************************************************************
11  * @file mss_util.h
12  * @author Microchip-FPGA Embedded Systems Solutions
13  * @brief MACROs defines and prototypes associated with utility functions
14  *
15  */
16 #ifndef MSS_UTIL_H
17 #define MSS_UTIL_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "encoding.h"
22 #include "mss_hart_ints.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /*
29  * Useful macros
30  */
31 #define WRITE_REG8(x, y) (*((volatile uint8_t *)(x)) = (y))
32 #define READ_REG8(x)     (*((volatile uint8_t *)(x)))
33 
34 #define WRITE_REG32(x, y) (*((volatile uint32_t *)(x)) = (y))
35 #define READ_REG32(x)     (*((volatile uint32_t *)(x)))
36 
37 #define WRITE_REG64(x, y) (*((volatile uint64_t *)(x)) = (y))
38 #define READ_REG64(x)     (*((volatile uint64_t *)(x)))
39 
40 /*
41  * return mcycle
42  */
43 uint64_t readmcycle(void);
44 
45 void sleep_ms(uint64_t msecs);
46 void sleep_cycles(uint64_t ncycles);
47 
48 
49 uint64_t get_stack_pointer(void);
50 uint64_t get_tp_reg(void);
51 uint64_t get_program_counter(void) __attribute__((aligned(16)));
52 
53 #ifdef MPFS_PRINTF_DEBUG_SUPPORTED
54 void display_address_of_interest(uint64_t * address_of_interest, int nb_locations);
55 #endif
56 
57 void exit_simulation(void);
58 
59 void enable_interrupts(void);
60 uint64_t disable_interrupts(void);
61 void restore_interrupts(uint64_t saved_psr);
62 void __disable_irq(void);
63 void __disable_all_irqs(void);
64 void __enable_irq(void);
65 void __enable_local_irq(uint8_t local_interrupt);
66 void __disable_local_irq(uint8_t local_interrupt);
67 
spinunlock(volatile long * pLock)68 static inline void spinunlock(volatile long *pLock)
69 {
70     __sync_lock_release(pLock);
71 }
72 
spinlock(volatile long * pLock)73 static inline void spinlock(volatile long *pLock)
74 {
75     while(__sync_lock_test_and_set(pLock, 1))
76     {
77         /* add yield if OS */
78 #if defined USING_FREERTOS
79         taskYIELD();
80 #endif
81     }
82 }
83 
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif  /* MSS_UTIL_H */
90