Implemented basic code for posix: open, close, sbrk, write, read, lseek, exit

This commit is contained in:
lcbcFoo 2018-02-27 15:21:44 -03:00
parent c9f61c9d40
commit ff2eae4ea1
15 changed files with 206 additions and 118 deletions

View file

@ -212,7 +212,9 @@ CONFIG_AHB_RROBIN=y
CONFIG_AHB_FPNPEN=y
CONFIG_AHB_IOADDR=FFF
CONFIG_APB_HADDR=800
# CONFIG_AHB_MON is not set
CONFIG_AHB_MON=y
# CONFIG_AHB_MONERR is not set
# CONFIG_AHB_MONWAR is not set
# CONFIG_AHB_DTRACE is not set
#
@ -271,7 +273,9 @@ CONFIG_DDR2SP_DELAY7=0
#
# On-chip RAM/ROM
#
# CONFIG_AHBROM_ENABLE is not set
CONFIG_AHBROM_ENABLE=y
CONFIG_AHBROM_START=000
# CONFIG_AHBROM_PIPE is not set
# CONFIG_AHBRAM_ENABLE is not set
#
@ -289,8 +293,8 @@ CONFIG_GRETH_FIFO8=y
# UARTs, timers and irq control
#
CONFIG_UART1_ENABLE=y
CONFIG_UA1_FIFO1=y
# CONFIG_UA1_FIFO2 is not set
# CONFIG_UA1_FIFO1 is not set
CONFIG_UA1_FIFO2=y
# CONFIG_UA1_FIFO4 is not set
# CONFIG_UA1_FIFO8 is not set
# CONFIG_UA1_FIFO16 is not set

View file

@ -35,8 +35,8 @@ entity ahbrom is
end;
architecture rtl of ahbrom is
constant abits : integer := 5;
constant bytes : integer := 20;
constant abits : integer := 4;
constant bytes : integer := 12;
constant dbits : integer := 32;
constant hconfig : ahb_config_type := (
@ -94,12 +94,10 @@ begin
comb : process (romaddr)
begin
case conv_integer(romaddr) is
when 16#00000# => romdata <= X"37010048";
when 16#00001# => romdata <= X"130101fe";
when 16#00002# => romdata <= X"b7070040";
when 16#00003# => romdata <= X"e7800700";
when 16#00004# => romdata <= X"13000000";
when 16#00005# => romdata <= X"00000000";
when 16#00000# => romdata <= X"b7070040";
when 16#00001# => romdata <= X"e7800700";
when 16#00002# => romdata <= X"13000000";
when 16#00003# => romdata <= X"00000000";
when others => romdata <= (others => '-');
end case;
end process;

View file

@ -202,7 +202,9 @@
#define CONFIG_AHB_FPNPEN 1
#define CONFIG_AHB_IOADDR FFF
#define CONFIG_APB_HADDR 800
#undef CONFIG_AHB_MON
#define CONFIG_AHB_MON 1
#undef CONFIG_AHB_MONERR
#undef CONFIG_AHB_MONWAR
#undef CONFIG_AHB_DTRACE
/*
* Debug Link
@ -255,7 +257,9 @@
/*
* On-chip RAM/ROM
*/
#undef CONFIG_AHBROM_ENABLE
#define CONFIG_AHBROM_ENABLE 1
#define CONFIG_AHBROM_START 000
#undef CONFIG_AHBROM_PIPE
#undef CONFIG_AHBRAM_ENABLE
/*
* Ethernet
@ -271,8 +275,8 @@
* UARTs, timers and irq control
*/
#define CONFIG_UART1_ENABLE 1
#define CONFIG_UA1_FIFO1 1
#undef CONFIG_UA1_FIFO2
#undef CONFIG_UA1_FIFO1
#define CONFIG_UA1_FIFO2 1
#undef CONFIG_UA1_FIFO4
#undef CONFIG_UA1_FIFO8
#undef CONFIG_UA1_FIFO16

View file

@ -91,7 +91,7 @@ package config is
constant CFG_FPNPEN : integer := 1;
constant CFG_AHBIO : integer := 16#FFF#;
constant CFG_APBADDR : integer := 16#800#;
constant CFG_AHB_MON : integer := 0;
constant CFG_AHB_MON : integer := 1;
constant CFG_AHB_MONERR : integer := 0;
constant CFG_AHB_MONWAR : integer := 0;
constant CFG_AHB_DTRACE : integer := 0;
@ -129,11 +129,11 @@ package config is
constant CFG_MIG_7SERIES : integer := 0;
constant CFG_MIG_7SERIES_MODEL : integer := 0;
-- AHB ROM
constant CFG_AHBROMEN : integer := 0;
constant CFG_AHBROMEN : integer := 1;
constant CFG_AHBROPIP : integer := 0;
constant CFG_AHBRODDR : integer := 16#000#;
constant CFG_ROMADDR : integer := 16#000#;
constant CFG_ROMMASK : integer := 16#E00# + 16#000#;
constant CFG_ROMADDR : integer := 16#100#;
constant CFG_ROMMASK : integer := 16#E00# + 16#100#;
-- AHB RAM
constant CFG_AHBRAMEN : integer := 0;
constant CFG_AHBRSZ : integer := 1;
@ -145,7 +145,7 @@ package config is
constant CFG_ETH_FIFO : integer := 8;
-- UART 1
constant CFG_UART1_ENABLE : integer := 1;
constant CFG_UART1_FIFO : integer := 1;
constant CFG_UART1_FIFO : integer := 2;
-- LEON3 interrupt controller
constant CFG_IRQ3_ENABLE : integer := 1;
constant CFG_IRQ3_NSEC : integer := 0;

View file

@ -1822,12 +1822,12 @@ begin
if r.a.ctrl.trap = '1' then tt := r.a.ctrl.tt;
elsif privileged_inst = '1' then tt := TT_PRIV;
elsif illegal_inst = '1' or r.a.decill = '1' then tt := TT_IINST;
elsif fp_disabled = '1' then tt := TT_FPDIS;
elsif cp_disabled = '1' then tt := TT_CPDIS;
--elsif fp_disabled = '1' then tt := TT_FPDIS;
--elsif cp_disabled = '1' then tt := TT_CPDIS;
elsif wph = '1' then tt := TT_WATCH;
elsif r.a.wovf= '1' then tt := TT_WINOF;
elsif r.a.wunf= '1' then tt := TT_WINUF;
elsif r.a.ticc= '1' then tt := TT_TICC;
--elsif r.a.wovf= '1' then tt := TT_WINOF;
--elsif r.a.wunf= '1' then tt := TT_WINUF;
--elsif r.a.ticc= '1' then tt := TT_TICC;
else trap := '0'; tt:= (others => '0'); end if;
end if;
end;
@ -2080,8 +2080,8 @@ end;
((ldcheck2 = '1') and (r.e.ctrl.rd = rfa2)))
then ldlock := '1'; end if;
de_fins_holdx := BPRED and fins and (r.a.bp or r.e.bp); -- skip BP on FPU inst in branch target address
de_fins_hold := de_fins_holdx;
-- de_fins_holdx := BPRED and fins and (r.a.bp or r.e.bp); -- skip BP on FPU inst in branch target address
-- de_fins_hold := de_fins_holdx;
ldlock := ldlock or y_hold or fpc_lock or (BPRED and r.a.bp) or de_fins_holdx;
if ((icc_check_bp and BPRED) = '1') and ((r.a.nobp or mul_hold) = '0') then
bp := bicc_hold_bp;
@ -2393,11 +2393,11 @@ end;
shcnt := iop2(4 downto 0); sari := '0'; shleft := '0'; invop2 := '0';
ymsb := iop1(0); mulins := '0'; divins := '0'; mulstep := '0';
macins := '0';
if r.e.ctrl.wy = '1' then y0 := my;
elsif r.m.ctrl.wy = '1' then y0 := r.m.y(0);
elsif r.x.ctrl.wy = '1' then y0 := r.x.y(0);
else y0 := r.w.s.y(0); end if;
--
-- if r.e.ctrl.wy = '1' then y0 := my;
-- elsif r.m.ctrl.wy = '1' then y0 := r.m.y(0);
-- elsif r.x.ctrl.wy = '1' then y0 := r.x.y(0);
-- else y0 := r.w.s.y(0); end if;
if r.e.ctrl.wicc = '1' then icc := me_icc;
elsif r.m.ctrl.wicc = '1' then icc := r.m.icc;

View file

@ -10,7 +10,7 @@
# Update the following variables to your environment and board used
# Cross compiler (Must be RV32I only)
CROSS=../../rv32i/rv32i-gnu/bin/riscv32-unknown-elf-
CROSS=../../rv32i-gcc/bin/riscv32-unknown-elf-
# Design you are targeting
DESIGN=leon3-digilent-nexys4ddr
@ -24,9 +24,9 @@ CC=gcc
AS=as
OBJCOPY=objcopy
CFLAGS=-nostdlib
OBJCPFLAGS=-O binary -j .text
OBJCPFLAGS=-O binary -j .text -j .data
LD_SCRIPT=ld_script
WRAP=-Wl,-wrap,exit,-wrap,open,-wrap,close,-wrap,write,-wrap,read
WRAP=-Wl,-wrap,exit,-wrap,open,-wrap,close,-wrap,write,-wrap,read,-wrap,lseek,-wrap,sbrk
#####################################################################
#
@ -34,8 +34,8 @@ WRAP=-Wl,-wrap,exit,-wrap,open,-wrap,close,-wrap,write,-wrap,read
#
#####################################################################
# Compiled binary linked with reonv_crt0
%.out : %.c reonv_crt0.o posix.o
${CROSS}${CC} -static -march=rv32i -T${LD_SCRIPT} $< -o $@
%.out : %.c reonv_crt0.o posix.o console.o
${CROSS}${CC} -static -march=rv32i ${WRAP} -T${LD_SCRIPT} $< -o $@
# Targets for .S.
# .o is for linking programs (i.e. crt0.S)
@ -43,6 +43,12 @@ WRAP=-Wl,-wrap,exit,-wrap,open,-wrap,close,-wrap,write,-wrap,read
%.hex : %.S
${CROSS}$(AS) $< -o $@
console.o: console.c
${CROSS}${CC} -c -static $< -o $@
reonv_print.o : reonv_print/console_dbg.c
${CROSS}${CC} -c -static -static-libgcc -lgcc $< -o $@
posix.o : posix.c
${CROSS}${CC} -c $< -o $@ -nostdlib ${WRAP}

Binary file not shown.

View file

@ -6,12 +6,12 @@
OUTPUT_FORMAT("elf32-littleriscv", "elf32-littleriscv",
"elf32-littleriscv")
OUTPUT_ARCH(riscv)
ENTRY(__reonv_startup)
ENTRY(__reonv_start)
SEARCH_DIR("/home/foo/IC/riscv-leon/rv32i/rv32i-gnu/riscv32-unknown-elf/lib");
SECTIONS
{
/* Read-only sections, merged into text segment: */
PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x40000000)); . = SEGMENT_START("text-segment", 0x40000000) + SIZEOF_HEADERS;
PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x40000000)); . = SEGMENT_START("text-segment", 0x40000000) + 0x1000;
.interp : { *(.interp) }
.note.gnu.build-id : { *(.note.gnu.build-id) }
.hash : { *(.hash) }
@ -54,16 +54,21 @@ SECTIONS
.iplt : { *(.iplt) }
.text :
{
/*
* Forced minimal ctr0 to be the first code
*/
reonv_crt0.o
posix.o
console.o
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
*(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
/* .gnu.warning sections are handled specially by elf32.em. */
*(.gnu.warning)
}

Binary file not shown.

View file

@ -1,9 +1,23 @@
int mul();
int fib();
#include <stdio.h>
int main(){
int i = 3;
int count = 0;
// int i[3] = {0xF1E2D3C4, 0x12345678, 0xACB45321};
//
int count = 0xDDDDDDDD;
// int j[3] = {0xECECECEC, 0xBBBBBBBB, 0x01010202};
int* array = (int*)sbrk(4 * sizeof(int));
int* array2 = (int*)sbrk(2 * sizeof(int));
int cc = 0xcccccccc;
array[0] = 0xAAAABBBB;
array[1] = 0xCCCCDDDD;
array[2] = 0xEEEEFFFF;
array[3] = 0x1111AAAA;
array2[0] = 0x2222BBBB;
array2[1] = 0x3333CCCC;
// Multiplies i by 4
//do{
@ -12,10 +26,15 @@ int main(){
// count++;
//}while(i < 2500);
i = fib(i);
//i = fib(i); // fib(16) = 987 = 0x3DB
//printf("i = \n", i);
return i;
//dbgleon_printf("%d", i);
// bsp_debug_uart_init();
// apbuart_write_polled(&i, sizeof(int));
// write(0,&i,3 * sizeof(int));
// lseek(0, -3*sizeof(int), SEEK_CUR);
// read(0, &j, 3 * sizeof(int));
}
int fib(int i){
@ -23,7 +42,3 @@ int fib(int i){
return 1;
return fib(i-1) + fib(i-2);
}
int mul(int i,int j){
return i*j;
}

View file

@ -1,82 +1,127 @@
#include <stdio.h>
// #include <stdio.h>
// #include <sys/types.h>
// #include <sys/stat.h>
// #include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include "reonv.h"
#undef errno
static char* heap = (char*) HEAP_START;
static char* out_mem = (char*)OUT_MEM_BEGIN;
extern int errno;
char *__env[1] = { 0 };
char **environ = __env;
// Currently only reads/writes data from output section of memory
#define SYS_RESTART 0x0
#define SYS_EXIT 0x1
#define SYS_FORK 0x2
#define SYS_READ 0x3
#define SYS_WRITE 0x4
#define SYS_OPEN 0x5
#define SYS_CLOSE 0x6
#define SYS_WAIT 0x7
#define SYS_CREAT 0x8
#define SYS_LINK 0x9
#define SYS_UNLINK 0xA
#define SYS_EXECVE 0xB
#define SYS_CHDIR 0xC
#define SYS_MKNOD 0xE
#define SYS_CHMOD 0xF
#define SYS_LCHOW 0x10
#define SYS_STAT 0x12
#define SYS_LSEEK 0x13
#define SYS_GETPID 0x14
#define SYS_ALARM 0x1B
#define SYS_FSTAT 0x6C
#define SYS_KILL 0x25
#define SYS_TIMES 0x2B
#define SYS_BRK 0x2D
#define SYS_GETTIMEOFDAY 0x4E
#define SYS_SLEEP 0xA2
#define SYS_ACCESS 0x21
#define DREADY 1
#define TREADY 4
volatile char *console = (char *) 0x80000100;
volatile int *uart_shifter = (int *) 0x80000104;
static unsigned *out_mem = (unsigned *)OUT_MEM_BEGIN;
static unsigned current_position = 0;
// extern void
// outbyte (int c)
// {
// volatile int *rxstat;
// volatile int *rxadata;
// int rxmask;
// while ((console[1] & TREADY) == 0);
// console[0] = (0x0ff & c);
// if (c == '\n')
// {
// while ((console[1] & TREADY) == 0);
// console[0] = (int) '\r';
// }
// }
//
// int
// inbyte (void)
// {
// if (!console)
// return (0);
// while (!(console[1] & DREADY));
// return console[0];
// }
// exit application
extern void _exit_c( int status ) {
void _exit_c( int status ) {
__asm("ebreak");
}
// Repositions the offset of the open file associated with the file descriptor fd to the argument offset.
int _lseek_c( int fd, int offset, int whence ) {
if(whence == SEEK_SET)
out_mem = (char*) (OUT_MEM_BEGIN + offset);
else if(whence == SEEK_CUR)
out_mem = (char*) (out_mem + offset);
else if(whence == SEEK_END)
out_mem = (char*) (OUT_MEM_END + offset);
else
return -1;
if(out_mem < (char*) OUT_MEM_BEGIN)
return -1;
else if(out_mem > (char*) OUT_MEM_END)
return -1;
return out_mem - (char*) OUT_MEM_BEGIN;
}
// Read from a file descriptor.
extern int _read_c( int fd, char *buffer, int len ) {
int _read_c( int fd, char *buffer, int len ) {
volatile register int r0 asm("t0") = fd;
volatile register char* r1 asm("t1") = buffer;
volatile register int r2 asm("t2") = len;
volatile register unsigned r7 asm("a0") = SYS_READ;
return r0;
int i;
for(i = 0; (i < len) && (&out_mem[i] < (char*)OUT_MEM_END); i++){
buffer[i] = out_mem[i];
}
out_mem += i;
return i;
}
// Write to a file descriptor.
extern int _write_c( int fd, char *buffer, int len ) {
int _write_c( int fd, char *buffer, int len ) {
volatile register int r0 asm("t0") = fd;
volatile register char* r1 asm("t1") = buffer;
volatile register int r2 asm("t2") = len;
volatile register unsigned r7 asm("a0") = SYS_WRITE;
return r0;
int i;
for(i = 0; (i < len) && (&out_mem[i] < (char*)OUT_MEM_END); i++){
//out_mem[i] = buffer[i];
(*console) = buffer[i];
while((*uart_shifter) & 0x2 != 0x2);
}
out_mem += i;
return i;
}
// Open a file.
extern int _open_c( const char *path, int flags, int mode ) {
int _open_c( const char *path, int flags, int mode ) {
volatile register const char* r0 asm("t0") = path;
volatile register int r1 asm("t1") = flags;
volatile register int r2 asm("t2") = mode;
volatile register unsigned r7 asm("a0") = SYS_OPEN;
return *(int*) r0;
return 0;
}
// Close a file descriptor.
extern int _close_c( int fd ) {
volatile register int r0 asm("t0") = fd;
volatile register unsigned r7 asm("a0") = SYS_CLOSE;
return r0;
return 0;
}
// Allocate space on heap
extern void* _sbrk_c( int incr ) {
void* addr = (void*) heap;
heap += incr;
return addr;
}

Binary file not shown.

View file

@ -1,4 +1,6 @@
#define RAM_START 0x40000000
#define RAM_SIZE 0x08000000
#define SP_START 0X43FFFFF0
#define OUT_MEM_BEGIN 0x45000000
#define SP_START 0x43FFFFF0
#define HEAP_START 0x43000000
#define OUT_MEM_BEGIN 0x44000000
#define OUT_MEM_END 0x45000000

View file

@ -3,24 +3,27 @@
.file "reonv_crt0.S"
.extern _start
.extern main
.extern _exit
.extern _open
.extern _close
.extern _write
.extern _read
.globl __reonv_startup
.type "__reonv_startup",@function
.type "exit",@function
.type "open",@function
.type "close",@function
.type "write",@function
.type "read",@function
.globl __reonv_start
.globl __wrap_write
.globl __wrap_exit
.globl __wrap_read
.globl __wrap_open
.globl __wrap_close
.globl __wrap_lseek
.globl __wrap_sbrk
.type "__reonv_start",@function
.type "__wrap_exit",@function
.type "__wrap_open",@function
.type "__wrap_close",@function
.type "__wrap_write",@function
.type "__wrap_read",@function
.type "__wrap_lseek",@function
.type "__wrap_sbrk", @function
.org 0x0
// Reset Function
__reonv_startup:
__reonv_start:
mv x1,x0
mv x2,x0
mv x3,x0
@ -59,16 +62,22 @@ __reonv_startup:
// Wrap some existent glibc posix functions used by benchmarkers
__wrap_exit:
call _exit_c
jal x0,_exit_c
__wrap_open:
call _open_c
jal x0,_open_c
__wrap_close:
call _close_c
jal x0,_close_c
__wrap_write:
call _write_c
jal x0,_write_c
__wrap_read:
call _read_c
jal x0,_read_c
__wrap_lseek:
jal x0,_lseek_c
__wrap_sbrk:
jal x0,_sbrk_c

Binary file not shown.