| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2023 Alessandro Gatti |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdint.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
#include "uart.h" |
| 32 |
|
| 33 |
extern void set_interrupt_table(void); |
| 34 |
extern int main(int argc, char **argv); |
| 35 |
|
| 36 |
void _entry_point(void) { |
| 37 |
// Set interrupt table |
| 38 |
set_interrupt_table(); |
| 39 |
// Enable UART |
| 40 |
uart_init(); |
| 41 |
// Now that we have a basic system up and running we can call main |
| 42 |
main(0, 0); |
| 43 |
// Finished |
| 44 |
exit(0); |
| 45 |
} |
| 46 |
|
| 47 |
void exit(int status) { |
| 48 |
uint32_t semihosting_arguments[2] = { 0 }; |
| 49 |
|
| 50 |
// Exit via QEMU's RISC-V semihosting support. |
| 51 |
__asm volatile ( |
| 52 |
".option push \n" // Transient options |
| 53 |
".option norvc \n" // Do not emit compressed instructions |
| 54 |
".align 4 \n" // 16 bytes alignment |
| 55 |
"mv a1, %0 \n" // Load buffer |
| 56 |
"li t0, 0x20026 \n" // ADP_Stopped_ApplicationExit |
| 57 |
"sw t0, 0(a1) \n" // ADP_Stopped_ApplicationExit |
| 58 |
"sw %1, 4(a1) \n" // Exit code |
| 59 |
"addi a0, zero, 0x20 \n" // TARGET_SYS_EXIT_EXTENDED |
| 60 |
"slli zero, zero, 0x1F \n" // Entry NOP |
| 61 |
"ebreak \n" // Give control to the debugger |
| 62 |
"srai zero, zero, 7 \n" // Semihosting call |
| 63 |
".option pop \n" // Restore previous options set |
| 64 |
: |
| 65 |
: "r" (semihosting_arguments), "r" (status) |
| 66 |
: "memory" |
| 67 |
); |
| 68 |
|
| 69 |
// Should never reach here. |
| 70 |
for (;;) { |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#ifndef NDEBUG |
| 75 |
void __assert_func(const char *file, int line, const char *func, const char *expr) { |
| 76 |
(void)func; |
| 77 |
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); |
| 78 |
exit(1); |
| 79 |
} |
| 80 |
#endif |
| 81 |
|
| 82 |
// Picolibc requires `stdout` to be explicitly defined. |
| 83 |
|
| 84 |
#ifdef _PICOLIBC__ |
| 85 |
FILE *const stdout; |
| 86 |
#endif |