Commit 0a181b66 authored by Pavel Pisa's avatar Pavel Pisa

seminaries/qtmips/os-emu-example: example and template for QtMips projects using C library.

Signed-off-by: Pavel Pisa's avatarPavel Pisa <pisa@cmp.felk.cvut.cz>
parent 017a75cf
ARCH=mips-elf
#ARCH=mips-linux-gnu
SOURCES = malloc-test.c qtmips_sys_stub.c crt0local.S
TARGET_EXE = malloc-test
SOURCES += qtmips_sys_stub.c crt0local.S
LDLIBS += -lc
CC=$(ARCH)-gcc
CXX=$(ARCH)-g++
AS=$(ARCH)-as
LD=$(ARCH)-ld
OBJCOPY=$(ARCH)-objcopy
#ARCHFLAGS += -fno-lto -static -specs=/opt/musl/mips-linux-gnu/lib/musl-gcc.specs
#ARCHFLAGS += -march=mips32
#ARCHFLAGS += -fno-lto
#ARCHFLAGS += -mno-shared
CFLAGS += -ggdb -Os -Wall
CXXFLAGS+= -ggdb -Os -Wall
AFLAGS += -ggdb
LDFLAGS += -ggdb
#LDFLAGS += -nostartfiles
LDFLAGS += -static
#LDFLAGS += -specs=/opt/musl/mips-linux-gnu/lib/musl-gcc.specs
CFLAGS += $(ARCHFLAGS)
CXXFLAGS+= $(ARCHFLAGS)
AFLAGS += $(ARCHFLAGS)
LDFLAGS += $(ARCHFLAGS)
OBJECTS += $(filter %.o,$(SOURCES:%.S=%.o))
OBJECTS += $(filter %.o,$(SOURCES:%.c=%.o))
OBJECTS += $(filter %.o,$(SOURCES:%.cpp=%.o))
all : default
.PHONY : default clean dep all run_test
%.o:%.S
$(CC) -D__ASSEMBLY__ $(AFLAGS) -c $< -o $@
%.o:%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
%.o:%.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
%.s:%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -S $< -o $@
default : $(TARGET_EXE)
#default : run_test
$(TARGET_EXE) : $(OBJECTS)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
dep: depend
depend: $(SOURCES) $(glob *.h)
echo '# autogenerated dependencies' > depend
ifneq ($(filter %.S,$(SOURCES)),)
$(CC) -D__ASSEMBLY__ $(AFLAGS) -w -E -M $(filter %.S,$(SOURCES)) \
>> depend
endif
ifneq ($(filter %.c,$(SOURCES)),)
$(CC) $(CFLAGS) $(CPPFLAGS) -w -E -M $(filter %.c,$(SOURCES)) \
>> depend
endif
ifneq ($(filter %.cpp,$(SOURCES)),)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -w -E -M $(filter %.cpp,$(SOURCES)) \
>> depend
endif
clean:
rm -f *.o *.a $(OBJECTS) $(TARGET_EXE) depend
-include depend
/* minimal replacement of crt0.o which is else provided by C library */
.globl main
.globl _start
.globl __start
.globl _heap_stack_start
.globl _heap_stack_end
.set noat
.set noreorder
.ent _start
.text
__start:
_start:
#if defined(__PIC__) || defined(__pic__)
bal next
nop
next:
.set noreorder
.cpload $31
.set reorder
#else
la $gp, _gp
#endif
la $sp, _heap_stack_end
addi $a0, $zero, 0
addi $a1, $zero, 0
jal main
nop
quit:
addi $a0, $zero, 0
addi $v0, $zero, 4001 /* SYS_exit */
syscall
loop: break
beq $zero, $zero, loop
nop
.end _start
.bss
_heap_stack_start:
.skip 0x800000
_heap_stack_end:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#define CHUNK_COUNT 10
static inline
uint32_t fill_value(int chunk, int idx)
{
return (chunk * 77) ^ idx * 55;
}
static inline
size_t chunk_len(int chunk)
{
return chunk * 137;
}
int main(void)
{
uint32_t *chunks[CHUNK_COUNT];
printf("Starting malloc-test\n");
for (int chunk = 0; chunk < CHUNK_COUNT; chunk++) {
size_t len = chunk_len(chunk);
uint32_t *p = malloc(len * sizeof(uint32_t));
chunks[chunk] = p;
if (!p) {
printf("FAILED alloc of chunk %d, len %d\n", chunk, (int)len);
exit(1);
} else {
printf("Alloc of chunk %d, len %d address %p\n", chunk, (int)len, p);
}
while (len--) {
*(p++) = fill_value(chunk, len);
}
}
for (int chunk = 0; chunk < CHUNK_COUNT; chunk++) {
size_t len = chunk_len(chunk);
uint32_t *p = chunks[chunk];
while (len--) {
if (*(p++) != fill_value(chunk, len)) {
printf("FAILED chunk %d, data compare\n", chunk);
exit(1);
}
}
printf("Chunk %d check OK\n", chunk);
}
printf("Succeed malloc-test\n");
return 0;
}
// ******************************************************************
// QtMips emulator https://github.com/cvut/QtMips support filees
//
// qtmips_regs.h - definition of the QtMips simulator peripherals
//
// (C) Copyright 2019 - 2020 by Pavel Pisa
// e-mail: pisa@cmp.felk.cvut.cz
// homepage: http://cmp.felk.cvut.cz/~pisa
// license: any combination of GPL, LGPL, MPL or BSD licenses
//
// ******************************************************************
#ifndef QTMIPS_REGS_H
#define QTMIPS_REGS_H
// Serial port/terminal registers
// There is mirror of this region at address 0xffff0000
// to match QtSpim and Mars emulators
#define SERIAL_PORT_BASE, 0xffffc000 // base address of serial port region
#define SERP_RX_ST_REG, 0xffffc000 // Receiver status register
#define SERP_RX_ST_REG_o, 0x0000 // Offset of RX_ST_REG
#define SERP_RX_ST_REG_READY_m, 0x1 // Data byte is ready to be read
#define SERP_RX_ST_REG_IE_m, 0x2 // Enable Rx ready interrupt
#define SERP_RX_DATA_REG, 0xffffc004 // Received data byte in 8 LSB bits
#define SERP_RX_DATA_REG_o, 0x0004 // Offset of RX_DATA_REG
#define SERP_TX_ST_REG, 0xffffc008 // Transmitter status register
#define SERP_TX_ST_REG_o, 0x0008 // Offset of TX_ST_REG
#define SERP_TX_ST_REG_READY_m, 0x1 // Transmitter can accept next byte
#define SERP_TX_ST_REG_IE_m, 0x2 // Enable Tx ready interrupt
#define SERP_TX_DATA_REG, 0xffffc00c // Write word to send 8 LSB bits to terminal
#define SERP_TX_DATA_REG_o, 0x000c // Offset of TX_DATA_REG
// Memory mapped peripheral for dial knobs input,
// LED and RGB LEDs output designed to match
// MZ_APO education Zynq based board developed
// by Petr Porazil and Pavel Pisa at PiKRON.com company
#define SPILED_REG_BASE 0xffffc100 // base of SPILED port region
#define SPILED_REG_LED_LINE 0xffffc104 // 32 bit word mapped as output
#define SPILED_REG_LED_LINE_o 0x0004 // Offset of the LED_LINE
#define SPILED_REG_LED_RGB1 0xffffc110 // RGB LED 1 color components
#define SPILED_REG_LED_RGB1_o 0x0010 // Offset of LED_RGB1
#define SPILED_REG_LED_RGB2 0xffffc114 // RGB LED 2 color components
#define SPILED_REG_LED_RGB2_o 0x0014 // Offset of LED_RGB2
#define SPILED_REG_KNOBS_8BIT 0xffffc124 // Three 8 bit knob values
#define SPILED_REG_KNOBS_8BIT_o 0x0024 // Offset of KNOBS_8BIT
// The simple 16-bit per pixel (RGB565) frame-buffer
// display size is 480 x 320 pixel
// Pixel format RGB565 expect
// bits 11 .. 15 red component
// bits 5 .. 10 green component
// bits 0 .. 4 blue component
#define LCD_FB_START 0xffe00000
#define LCD_FB_END 0xffe4afff
// Mapping of interrupts
// Irq number Cause/Status Bit Source
// 2 / HW0 10 Serial port ready to accept character to Tx
// 3 / HW1 11 There is received character ready to be read
// 7 / HW5 15 Counter reached value in Compare register
#endif /*QTMIPS_REGS_H*/
/* Support files for GNU libc. Files in the system namespace go here.
Files in the C namespace (ie those that do not start with an
underscore) go in .c. */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <errno.h>
#include <reent.h>
#include "qtmips_unistd.h"
#define UNUSED_PARAM(symbol) ((void)(symbol))
/* Register name - works in collusion with the linker. */
register char * stack_ptr asm ("sp");
int _read(int file, char *ptr, int len);
int
_read (int file,
char * ptr,
int len)
{
register int __v0 asm("$v0") = __NR_read;
register int __a0 asm("$a0") = file;
register char * __a1 asm("$a1") = ptr;
register int __a2 asm("$a2") = len;
register int __a3 asm("$a3");
__asm__ volatile (
".set\tnoreorder\n\t"
"syscall\n\t"
".set reorder"
: "=r" (__v0), "=r" (__a3)
: "r" (__v0), "r" (__a0), "r" (__a1), "r"(__a2)
: );
return __a3? -1: __v0;
}
int _write(int file, const char *ptr, int len);
int
_write (int file,
const char * ptr,
int len)
{
register int __v0 asm("$v0") = __NR_write;
register int __a0 asm("$a0") = file;
register const char * __a1 asm("$a1") = ptr;
register int __a2 asm("$a2") = len;
register int __a3 asm("$a3");
__asm__ volatile (
".set\tnoreorder\n\t"
"syscall\n\t"
".set reorder"
: "=r" (__v0), "=r" (__a3)
: "r" (__v0), "r" (__a0), "r" (__a1), "r"(__a2)
: );
return __a3? -1: __v0;
}
int
_lseek (int file,
int pos,
int dir)
{
UNUSED_PARAM(file);
UNUSED_PARAM(pos);
UNUSED_PARAM(dir);
return -1;
}
int _open(const char *path, int flags, ...);
int
_open (const char * path,
int flags,
...)
{
register int __v0 asm("$v0") = __NR_open;
register const char * __a0 asm("$a0") = path;
register int __a1 asm("$a1") = flags;
register int __a2 asm("$a2") = 0;
register int __a3 asm("$a3");
if (flags & O_CREAT) {
va_list list;
va_start(list, flags);
__a2 = va_arg(list, int);
va_end(list);
}
__asm__ volatile (
".set\tnoreorder\n\t"
"syscall\n\t"
".set reorder"
: "=r" (__v0), "=r" (__a3)
: "r" (__v0), "r" (__a0), "r" (__a1), "r"(__a2)
: );
return __a3? -1: __v0;
}
int _close(int file);
int
_close (int file)
{
register int __v0 asm("$v0") = __NR_close;
register int __a0 asm("$a0") = file;
register int __a3 asm("$a3");
__asm__ volatile (
".set\tnoreorder\n\t"
"syscall\n\t"
".set reorder"
: "=r" (__v0), "=r" (__a3)
: "r" (__v0), "r" (__a0)
: );
return __a3? -1: __v0;
}
void _exit(int n);
void
_exit (int n)
{
register int __v0 asm("$v0") = __NR_exit;
register int __a0 asm("$a0") = n;
while(1) __asm__ volatile (
".set\tnoreorder\n\t"
"syscall\n\t"
".set reorder"
:
: "r" (__v0), "r" (__a0)
: "a3");
}
void abort(void);
void abort(void)
{
while(1) __asm__ volatile (
".set\tnoreorder\n\t"
"break\n\t"
".set reorder"
:
:
: );
}
int _kill(int n, int m);
int
_kill (int n, int m)
{
UNUSED_PARAM(n);
UNUSED_PARAM(m);
return -1;
}
int _getpid(void);
int
_getpid (void)
{
return 1;
}
void *_sbrk(ptrdiff_t incr);
void *
_sbrk (ptrdiff_t incr)
{
extern char _heap_stack_start;
static char *heap_end;
char * prev_heap_end;
incr=(incr+3) & ~3;
if (heap_end == NULL)
heap_end = & _heap_stack_start;
prev_heap_end = heap_end;
if ((heap_end + incr > stack_ptr) && (&_heap_stack_start < stack_ptr))
{
/* Some of the libstdc++-v3 tests rely upon detecting
out of memory errors, so do not abort here. */
#if 1
extern void abort (void);
_write (1, "_sbrk: Heap and stack collision\n", 32);
abort ();
#else
errno = ENOMEM;
return (caddr_t) -1;
#endif
}
heap_end += incr;
return /*(caddr_t)*/ prev_heap_end;
}
int _fstat(int file, struct stat *st);
int
_fstat (int file, struct stat * st)
{
UNUSED_PARAM(file);
UNUSED_PARAM(st);
return -1;
}
int _stat (const char *fname, struct stat *st);
int _stat (const char *fname, struct stat *st)
{
UNUSED_PARAM(fname);
UNUSED_PARAM(st);
return -1;
}
int _link(const char *path1, const char *path2);
int
_link (const char *path1,
const char *path2)
{
UNUSED_PARAM(path1);
UNUSED_PARAM(path2);
return -1;
}
int _unlink(const char *path);
int
_unlink (const char *path)
{
UNUSED_PARAM(path);
return -1;
}
void _raise(void);
void
_raise (void)
{
return;
}
int _gettimeofday(struct timeval *tp, struct timezone *tzp);
int
_gettimeofday (struct timeval *tp,
struct timezone *tzp)
{
if(tp)
{
tp->tv_sec = 0;
tp->tv_usec = 0;
}
/* Return fixed data for the timezone. */
if (tzp)
{
tzp->tz_minuteswest = 0;
tzp->tz_dsttime = 0;
}
return 0;
}
/* Return a clock that ticks at 100Hz. */
clock_t _times(struct tms *tp);
clock_t
_times (struct tms * tp)
{
clock_t timeval = 0;
if (tp)
{
tp->tms_utime = timeval; /* user time */
tp->tms_stime = 0; /* system time */
tp->tms_cutime = 0; /* user time, children */
tp->tms_cstime = 0; /* system time, children */
}
return timeval;
};
int isatty(int fd);
int
isatty (int fd)
{
UNUSED_PARAM(fd);
return 1;
}
int _system(const char *s);
int
_system (const char *s)
{
UNUSED_PARAM(s);
return -1;
}
int _rename(const char *oldpath, const char *newpath);
int
_rename (const char * oldpath, const char * newpath)
{
UNUSED_PARAM(oldpath);
UNUSED_PARAM(newpath);
return -1;
}
/* extern (.*) ([^ ]*) _PARAMS \(\(struct _reent \*(,*)(.*)\)\); */
/* \1 \2 (struct _reent *\3\4) { return \2(\4);} */
struct _reent;
int _close_r(struct _reent *reent, int file)
{ UNUSED_PARAM(reent); return _close(file);}
/*
int _fcntl_r(struct _reent *, int, int, int)
{ return _fcntl( int, int, int);}
*/
int _fstat_r(struct _reent *reent, int file, struct stat *st);
int _fstat_r(struct _reent *reent, int file, struct stat *st)
{ UNUSED_PARAM(reent); return _fstat(file, st);}
int _getpid_r(struct _reent *reent);
int _getpid_r(struct _reent *reent)
{ UNUSED_PARAM(reent); return _getpid();}
int _kill_r(struct _reent *reent, int n, int m);
int _kill_r(struct _reent *reent, int n, int m)
{ UNUSED_PARAM(reent); return _kill(n, m);}
int _link_r(struct _reent *reent, const char *path1, const char *path2);
int _link_r(struct _reent *reent, const char *path1, const char *path2)
{ UNUSED_PARAM(reent); return _link(path1, path2);}
_off_t _lseek_r(struct _reent *reent, int file, _off_t pos, int dir);
_off_t _lseek_r(struct _reent *reent, int file, _off_t pos, int dir)
{ UNUSED_PARAM(reent); return _lseek(file, pos, dir);}
int _open_r(struct _reent *reent, const char *path, int flags, int opts);
int _open_r(struct _reent *reent, const char *path, int flags, int opts)
{ UNUSED_PARAM(reent); return _open(path, flags, opts);}
_ssize_t _read_r(struct _reent *reent, int file, void *ptr, size_t len);
_ssize_t _read_r(struct _reent *reent, int file, void *ptr, size_t len)
{ UNUSED_PARAM(reent); return _read(file, ptr, len);}
void *_sbrk_r(struct _reent *reent, ptrdiff_t incr);
void *_sbrk_r(struct _reent *reent, ptrdiff_t incr)
{ UNUSED_PARAM(reent); return _sbrk(incr);}
int _stat_r(struct _reent *reent, const char *path, struct stat *st);
int _stat_r(struct _reent *reent, const char *path, struct stat *st)
{ UNUSED_PARAM(reent); return _stat(path, st);}
int _unlink_r(struct _reent *reent, const char *path);
int _unlink_r(struct _reent *reent, const char *path)
{ UNUSED_PARAM(reent); return _unlink(path);}
_ssize_t _write_r(struct _reent *reent, int file, const void *ptr, size_t len);
_ssize_t _write_r(struct _reent *reent, int file, const void *ptr, size_t len)
{ UNUSED_PARAM(reent); return _write(file, ptr, len);}
/* FIXME: TDK added dummy functions. Need to implement. */