Skip to content

Commit

Permalink
[+] Restore cpp_demo example enabling newlib stub into common/system
Browse files Browse the repository at this point in the history
[+] Update helloworld example: speedup inititialization using memcpy, use common code
  • Loading branch information
sergeykhbr committed Nov 22, 2023
1 parent b4daf94 commit 6d3811c
Show file tree
Hide file tree
Showing 30 changed files with 40,269 additions and 41,332 deletions.
36 changes: 23 additions & 13 deletions examples/common/system/gcc_newlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
*/

#include <inttypes.h>
#include "utils.h"

extern char __heap_start__[]; // defined in linker script
int heap_allocated_sz_ = 0;

/*
* @brief Increase program data space.
* @details Malloc and related functions depend on this
*/
char * __attribute__((weak)) _sbrk(int incr) {
return __heap_start__;
char * sbrk(int incr) {
char *ret = __heap_start__ + heap_allocated_sz_;
incr = (incr + 3) & (~3); // align value to 4
heap_allocated_sz_ += incr;
return ret;
}


Expand All @@ -33,20 +38,26 @@ char * __attribute__((weak)) _sbrk(int incr) {
* to all files, including stdout
* @return -1 on error or number of bytes sent
*/
int _write(int file __attribute__((unused)),
int write(int file __attribute__((unused)),
const void *ptr __attribute__((unused)),
int len)
{
uint8_t *buf = (uint8_t *)ptr;
int total = len;
while (total--) {
utils_uart_putc(*buf++);
}

return len;
}

int _close(int file __attribute__((unused)))
int close(int file __attribute__((unused)))
{
return -1;
}

#if!defined(_WIN32)
void __attribute__((weak)) _exit(int code __attribute__((unused)))
void exit(int code __attribute__((unused)))
{
// TODO: write on trace
while (1) {}
Expand All @@ -59,28 +70,27 @@ void __attribute__((weak)) _exit(int code __attribute__((unused)))
all files are regarded as character special devices.
The `sys/stat.h' header file required is distributed in the `include' subdirectory for this C library.
*/
int _fstat(int file __attribute__((unused)),
int fstat(int file __attribute__((unused)),
void *st __attribute__((unused)))
{
return 0;
}

int __attribute__((weak))_getpid(void)
int getpid(void)
{
return -1;
return -1;
}

/*
isatty
Query whether output stream is a terminal. For consistency with the other minimal implementations,
*/
int _isatty(int file __attribute__((unused)))
int isatty(int file __attribute__((unused)))
{
return 1;
}

int __attribute__((weak))
_kill(int pid __attribute__((unused)), int sig __attribute__((unused)))
int kill(int pid __attribute__((unused)), int sig __attribute__((unused)))
{
return -1;
}
Expand All @@ -89,7 +99,7 @@ _kill(int pid __attribute__((unused)), int sig __attribute__((unused)))
lseek
Set position in a file. Minimal implementation:
*/
int _lseek(int file __attribute__((unused)),
int lseek(int file __attribute__((unused)),
int ptr __attribute__((unused)),
int dir __attribute__((unused)))
{
Expand All @@ -103,7 +113,7 @@ int _lseek(int file __attribute__((unused)),
* @return -1 on error or blocks until the number of characters have been
read.
*/
int _read(int file __attribute__((unused)),
int read(int file __attribute__((unused)),
void *ptr __attribute__((unused)),
int len __attribute__((unused)))
{
Expand Down
26 changes: 11 additions & 15 deletions examples/common/system/gcc_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <inttypes.h>
#include "utils.h"

extern void main(void);

Expand All @@ -40,12 +41,8 @@ void init_data_bss(void)
{
/* Declare pointers for various data sections. These pointers
* are initialized using values pulled in from the linker file */
uint8_t * bss_start;
const uint8_t * bss_end;

/* BSS */
bss_start = (uint8_t *)__bss_start__;
bss_end = (uint8_t *)__bss_end__;
uint8_t * bss_start = (uint8_t *)__bss_start__;
const uint8_t * bss_end = (uint8_t *)__bss_end__;

/* Clear the zero-initialized data section */
while (bss_end != bss_start) {
Expand Down Expand Up @@ -83,30 +80,29 @@ __run_fini_array (void)

count = __fini_array_end - __fini_array_start;

for (i = count; i > 0; i--)
{
for (i = count; i > 0; i--) {
__fini_array_start[i - 1] ();
}
}

void copy_data_section(void)
{
for (size_t i = 0; i < (size_t)__DATA_SIZE; i++) {
__DATA_START[i] = __DATA_LOAD_START[i];
}
void copy_data_section(void) {
utils_memcpy(__DATA_START, __DATA_LOAD_START, (int)__DATA_SIZE);
// for (size_t i = 0; i < (size_t)__DATA_SIZE; i++) {
// __DATA_START[i] = __DATA_LOAD_START[i];
// }
}


int __main(void)
{
init_data_bss();

copy_data_section();

// Call the standard library initialisation (mandatory for C++ to
// execute the constructors for the static objects).
__run_init_array();

copy_data_section();

// Call the main entry point, and save the exit code.
main();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Sergey Khabarov, sergeykhbr@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

OUTPUT_ARCH( "riscv" )

ENTRY(__VECTOR_TABLE)
Expand Down
32 changes: 32 additions & 0 deletions examples/common/system/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 Sergey Khabarov, sergeykhbr@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <inttypes.h>
#include <axi_maps.h>
#include <string.h>

void utils_memcpy(void *dst, void *src, int sz) {
memcpy(dst, src, sz);
}

void utils_uart_putc(char s) {
uart_map *uart = (uart_map *)ADDR_BUS0_XSLV_UART0;
uart_txdata_type txdata;
do {
txdata.v = uart->txdata;
} while (txdata.b.full);
uart->txdata = s;
}
22 changes: 9 additions & 13 deletions examples/cpp_demo/src/general.h → examples/common/system/utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
* Copyright 2023 Sergey Khabarov, sergeykhbr@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,24 +14,20 @@
* limitations under the License.
*/

#pragma once

#include <inttypes.h>
#include <stdarg.h>

#ifndef __TEST_NORF_SRC_GENERAL_H__
#define __TEST_NORF_SRC_GENERAL_H__

#ifdef __cplusplus
extern "C" {
#if defined(__cplusplus)
extern "C"
{
#endif

void print_uart(const char *buf, int sz);
void print_uart_hex(uint64_t val);
void printf_uart(const char *fmt, ... );
void utils_memcpy(void *dst, void *src, int sz);

void print_pnp(void);
void utils_uart_putc(char s);

#ifdef __cplusplus
#if defined(__cplusplus)
} // extern "C"
#endif

#endif // __TEST_NORF_SRC_GENERAL_H__
Loading

0 comments on commit 6d3811c

Please sign in to comment.