klib: printf support
This commit is contained in:
parent
f38674ce79
commit
955f1f2d79
19 changed files with 438 additions and 245 deletions
|
@ -10,14 +10,16 @@ set(SOURCES
|
|||
ioe/disk.c
|
||||
ioe/gpu.c
|
||||
ioe/input.c
|
||||
ioe/timer.c
|
||||
)
|
||||
ioe/timer.c)
|
||||
add_library(am-native ${SOURCES})
|
||||
|
||||
# FIXME: get free(): invalid address when user program compiled without pie
|
||||
set_target_properties(am-native PROPERTIES
|
||||
POSITION_INDEPENDENT_CODE TRUE
|
||||
INTERFACE_POSITION_INDEPENDENT_CODE TRUE)
|
||||
set_target_properties(
|
||||
am-native PROPERTIES POSITION_INDEPENDENT_CODE TRUE
|
||||
INTERFACE_POSITION_INDEPENDENT_CODE TRUE)
|
||||
|
||||
find_package(SDL2 REQUIRED)
|
||||
target_link_libraries(am-native PUBLIC SDL2::SDL2 PRIVATE klib_interface am_interface)
|
||||
target_link_libraries(
|
||||
am-native
|
||||
PUBLIC SDL2::SDL2
|
||||
PRIVATE klib_interface am_interface)
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
#include <am.h>
|
||||
#include <nemu.h>
|
||||
|
||||
void __am_timer_init() {
|
||||
}
|
||||
void __am_timer_init() {}
|
||||
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *uptime) {
|
||||
uptime->us = 0;
|
||||
uptime->us = ((uint64_t)inl(RTC_ADDR + 4) << 32) + inl(RTC_ADDR);
|
||||
}
|
||||
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *rtc) {
|
||||
rtc->second = 0;
|
||||
rtc->minute = 0;
|
||||
rtc->hour = 0;
|
||||
rtc->day = 0;
|
||||
rtc->month = 0;
|
||||
rtc->year = 1900;
|
||||
rtc->hour = 0;
|
||||
rtc->day = 0;
|
||||
rtc->month = 0;
|
||||
rtc->year = 1900;
|
||||
}
|
||||
|
|
|
@ -6,19 +6,18 @@ int main(const char *args);
|
|||
|
||||
Area heap = RANGE(&_heap_start, PMEM_END);
|
||||
#ifndef MAINARGS
|
||||
#define MAINARGS ""
|
||||
#define MAINARGS "5"
|
||||
#endif
|
||||
static const char mainargs[] = MAINARGS;
|
||||
|
||||
void putch(char ch) {
|
||||
outb(SERIAL_PORT, ch);
|
||||
}
|
||||
void putch(char ch) { outb(SERIAL_PORT, ch); }
|
||||
|
||||
void halt(int code) {
|
||||
nemu_trap(code);
|
||||
|
||||
// should not reach here
|
||||
while (1);
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
||||
void _trm_init() {
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
foreach(PLATFORM IN LISTS PLATFORMS)
|
||||
string(TOUPPER ${ARCH} ARCH_UPPER)
|
||||
set(AM_COMMON_COMPILE_DEF
|
||||
# -- Arch related
|
||||
$<MAKE_C_IDENTIFIER:__ARCH_${ARCH_UPPER}__>
|
||||
__ISA_${ISA_UPPER}__
|
||||
__PLATFORM_${PLATFORM_UPPER}__
|
||||
|
||||
$<$<BOOL:${NATIVE_USE_KLIB}>:__NATIVE_USE_KLIB__>
|
||||
)
|
||||
add_subdirectory(${PLATFORM})
|
||||
string(TOUPPER ${ARCH} ARCH_UPPER)
|
||||
set(AM_COMMON_COMPILE_DEF
|
||||
# -- Arch related
|
||||
$<MAKE_C_IDENTIFIER:__ARCH_${ARCH_UPPER}__> __ISA_${ISA_UPPER}__
|
||||
__PLATFORM_${PLATFORM_UPPER}__
|
||||
$<$<BOOL:${NATIVE_USE_KLIB}>:__NATIVE_USE_KLIB__>)
|
||||
add_subdirectory(${PLATFORM})
|
||||
endforeach()
|
||||
|
|
|
@ -1,52 +1,41 @@
|
|||
include(nemu-settings)
|
||||
include(riscv-settings)
|
||||
|
||||
add_library(am-riscv-nemu
|
||||
cte.c
|
||||
start.S
|
||||
trap.S
|
||||
vme.c
|
||||
${NEMU_SOURCES}
|
||||
)
|
||||
add_library(am-riscv-nemu cte.c start.S trap.S vme.c ${NEMU_SOURCES})
|
||||
|
||||
target_compile_options(am-riscv-nemu PRIVATE
|
||||
${NEMU_COMPILE_OPTIONS}
|
||||
${RISCV_COMPILE_OPTIONS})
|
||||
target_compile_options(am-riscv-nemu PRIVATE ${NEMU_COMPILE_OPTIONS}
|
||||
${RISCV_COMPILE_OPTIONS})
|
||||
|
||||
target_link_options(am-riscv-nemu PRIVATE
|
||||
${NEMU_LINK_OPITIONS}
|
||||
${RISCV_LINK_OPTIONS})
|
||||
target_link_options(am-riscv-nemu PRIVATE ${NEMU_LINK_OPITIONS}
|
||||
${RISCV_LINK_OPTIONS})
|
||||
|
||||
target_include_directories(am-riscv-nemu PRIVATE
|
||||
${NEMU_INCLUDE_DIRECTORIES})
|
||||
target_include_directories(am-riscv-nemu PRIVATE ${NEMU_INCLUDE_DIRECTORIES})
|
||||
|
||||
target_link_options(am-riscv-nemu INTERFACE
|
||||
LINKER:--defsym=_pmem_start=0x80000000
|
||||
LINKER:--defsym=_entry_offset=0x0
|
||||
LINKER:--gc-sections
|
||||
LINKER:-e _start
|
||||
-nostartfiles)
|
||||
target_link_options(
|
||||
am-riscv-nemu
|
||||
INTERFACE
|
||||
LINKER:--defsym=_pmem_start=0x80000000
|
||||
LINKER:--defsym=_entry_offset=0x0
|
||||
LINKER:--gc-sections
|
||||
LINKER:-e
|
||||
_start
|
||||
-nostartfiles)
|
||||
|
||||
target_link_options(am-riscv-nemu INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
target_link_options(
|
||||
am-riscv-nemu INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
|
||||
target_include_directories(am-riscv-nemu
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/am/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine>)
|
||||
target_link_libraries(
|
||||
am-riscv-nemu
|
||||
PUBLIC am_interface klib_interface
|
||||
INTERFACE m)
|
||||
|
||||
target_link_libraries(am-riscv-nemu
|
||||
PUBLIC am_interface klib_interface
|
||||
INTERFACE m)
|
||||
target_compile_definitions(am-riscv-nemu PRIVATE ISA_H=<riscv/riscv.h>)
|
||||
|
||||
target_compile_definitions(am-riscv-nemu PRIVATE
|
||||
ISA_H=<riscv/riscv.h>)
|
||||
|
||||
set_target_properties(am-riscv-nemu PROPERTIES
|
||||
POSITION_INDEPENDENT_CODE OFF
|
||||
INTERFACE_POSITION_INDEPENDENT_CODE OFF)
|
||||
set_target_properties(
|
||||
am-riscv-nemu PROPERTIES POSITION_INDEPENDENT_CODE OFF
|
||||
INTERFACE_POSITION_INDEPENDENT_CODE OFF)
|
||||
|
||||
install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR})
|
||||
|
||||
|
|
|
@ -2,38 +2,45 @@ include(riscv-settings)
|
|||
|
||||
add_subdirectory(libgcc)
|
||||
|
||||
add_library(am-riscv-npc
|
||||
cte.c
|
||||
input.c
|
||||
ioe.c
|
||||
mpe.c
|
||||
start.S
|
||||
timer.c
|
||||
trap.S
|
||||
trm.c
|
||||
vme.c
|
||||
)
|
||||
add_library(
|
||||
am-riscv-npc
|
||||
cte.c
|
||||
input.c
|
||||
ioe.c
|
||||
mpe.c
|
||||
start.S
|
||||
timer.c
|
||||
trap.S
|
||||
trm.c
|
||||
vme.c)
|
||||
|
||||
target_link_libraries(am-riscv-npc PRIVATE npcgcc PUBLIC am_interface klib_interface)
|
||||
target_link_libraries(
|
||||
am-riscv-npc
|
||||
PRIVATE npcgcc
|
||||
PUBLIC am_interface klib_interface)
|
||||
|
||||
target_link_options(am-riscv-npc INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
target_link_options(
|
||||
am-riscv-npc INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
|
||||
target_link_options(am-riscv-npc INTERFACE
|
||||
LINKER:--defsym=_pmem_start=0x80000000
|
||||
LINKER:--defsym=_entry_offset=0x0
|
||||
LINKER:--gc-sections
|
||||
LINKER:-e _start
|
||||
-nostartfiles)
|
||||
target_link_options(
|
||||
am-riscv-npc
|
||||
INTERFACE
|
||||
LINKER:--defsym=_pmem_start=0x80000000
|
||||
LINKER:--defsym=_entry_offset=0x0
|
||||
LINKER:--gc-sections
|
||||
LINKER:-e
|
||||
_start
|
||||
-nostartfiles)
|
||||
|
||||
target_link_options(am-riscv-npc INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
target_link_options(
|
||||
am-riscv-npc INTERFACE
|
||||
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
|
||||
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
|
||||
|
||||
target_compile_definitions(am-riscv-npc
|
||||
PUBLIC ${AM_COMMON_COMPILE_DEF} ARCH_H=<arch/riscv.h>
|
||||
)
|
||||
target_compile_definitions(am-riscv-npc PUBLIC ${AM_COMMON_COMPILE_DEF}
|
||||
ARCH_H=<arch/riscv.h>)
|
||||
|
||||
install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR})
|
||||
|
|
|
@ -7,26 +7,32 @@ void __am_timer_rtc(AM_TIMER_RTC_T *);
|
|||
void __am_timer_uptime(AM_TIMER_UPTIME_T *);
|
||||
void __am_input_keybrd(AM_INPUT_KEYBRD_T *);
|
||||
|
||||
static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) { cfg->present = true; cfg->has_rtc = true; }
|
||||
static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; }
|
||||
static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) {
|
||||
cfg->present = true;
|
||||
cfg->has_rtc = true;
|
||||
}
|
||||
static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; }
|
||||
static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; }
|
||||
|
||||
typedef void (*handler_t)(void *buf);
|
||||
static void *lut[128] = {
|
||||
[AM_TIMER_CONFIG] = __am_timer_config,
|
||||
[AM_TIMER_RTC ] = __am_timer_rtc,
|
||||
[AM_TIMER_UPTIME] = __am_timer_uptime,
|
||||
[AM_INPUT_CONFIG] = __am_input_config,
|
||||
[AM_INPUT_KEYBRD] = __am_input_keybrd,
|
||||
[AM_UART_CONFIG] = __am_uart_config,
|
||||
[AM_TIMER_CONFIG] = __am_timer_config,
|
||||
[AM_TIMER_RTC] = __am_timer_rtc,
|
||||
[AM_TIMER_UPTIME] = __am_timer_uptime,
|
||||
[AM_INPUT_CONFIG] = __am_input_config,
|
||||
[AM_INPUT_KEYBRD] = __am_input_keybrd,
|
||||
};
|
||||
|
||||
static void fail(void *buf) { panic("access nonexist register"); }
|
||||
|
||||
bool ioe_init() {
|
||||
for (int i = 0; i < LENGTH(lut); i++)
|
||||
if (!lut[i]) lut[i] = fail;
|
||||
if (!lut[i])
|
||||
lut[i] = fail;
|
||||
__am_timer_init();
|
||||
return true;
|
||||
}
|
||||
|
||||
void ioe_read (int reg, void *buf) { ((handler_t)lut[reg])(buf); }
|
||||
void ioe_read(int reg, void *buf) { ((handler_t)lut[reg])(buf); }
|
||||
void ioe_write(int reg, void *buf) { ((handler_t)lut[reg])(buf); }
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
add_library(npcgcc
|
||||
ashldi3.c
|
||||
div.S
|
||||
muldi3.S
|
||||
multi3.c
|
||||
unused.c
|
||||
)
|
||||
add_library(npcgcc ashldi3.c div.S muldi3.S multi3.c unused.c)
|
||||
|
||||
target_link_libraries(npcgcc PRIVATE klib_interface am_interface)
|
||||
target_link_options(npcgcc INTERFACE -nolibc -nostdlib)
|
||||
|
||||
install(TARGETS npcgcc
|
||||
EXPORT amTargets-riscv-npc
|
||||
LIBRARY DESTINATION lib)
|
||||
install(
|
||||
TARGETS npcgcc
|
||||
EXPORT amTargets-riscv-npc
|
||||
LIBRARY DESTINATION lib)
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
#include "npc.h"
|
||||
#include <am.h>
|
||||
#include <riscv/riscv.h>
|
||||
|
||||
void __am_timer_init() {
|
||||
}
|
||||
void __am_timer_init() {}
|
||||
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *uptime) {
|
||||
uptime->us = 0;
|
||||
uptime->us = ((uint64_t)inl(RTC_ADDR + 4) << 32) + inl(RTC_ADDR);
|
||||
}
|
||||
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *rtc) {
|
||||
rtc->second = 0;
|
||||
rtc->minute = 0;
|
||||
rtc->hour = 0;
|
||||
rtc->day = 0;
|
||||
rtc->month = 0;
|
||||
rtc->year = 1900;
|
||||
rtc->hour = 0;
|
||||
rtc->day = 0;
|
||||
rtc->month = 0;
|
||||
rtc->year = 1900;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "npc.h"
|
||||
#include <am.h>
|
||||
#include <klib-macros.h>
|
||||
#include <riscv/riscv.h>
|
||||
|
||||
extern char _heap_start;
|
||||
int main(const char *args);
|
||||
|
@ -10,11 +12,11 @@ extern char _pmem_start;
|
|||
|
||||
Area heap = RANGE(&_heap_start, PMEM_END);
|
||||
#ifndef MAINARGS
|
||||
#define MAINARGS ""
|
||||
#define MAINARGS "3"
|
||||
#endif
|
||||
static const char mainargs[] = MAINARGS;
|
||||
|
||||
void putch(char ch) {}
|
||||
void putch(char ch) { outb(SERIAL_PORT, ch); }
|
||||
|
||||
void halt(int code) {
|
||||
asm volatile("mv a0, %0; ebreak" : : "r"(code));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue