- libsys multitarget
- apps multitarget

git-svn-id: http://moon:8086/svn/mips@67 a8ebac50-d88d-4704-bea3-6648445a41b3
This commit is contained in:
2017-01-10 17:50:12 +00:00
parent a67ef9649d
commit 82802139cd
18 changed files with 947 additions and 807 deletions
+13 -621
View File
@@ -5,21 +5,15 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "console.h"
#include "libsys.h"
// ---------------------------------------------------------------------------------
// Forward declarations
// ---------------------------------------------------------------------------------
void flush(uint32_t port);
uint32_t GetPort(int file);
// ---------------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------------
void libsys_init(void)
{
uint32_t volatile *pVGA_ctrl = (uint32_t*)SYS_VGA_CTRL;
uint32_t volatile *pITIM_ctrl = (uint32_t*)SYS_ITIM_CTRL;
// Screen_clr();
@@ -40,27 +34,12 @@ void libsys_init(void)
dbg_init();
// flush stdin
// flush(GetPort(0));
// flush(con_getPort(0));
// Do some initializations here
}
enum
{
PORT_IN_UART_0 = 0,
PORT_IN_UART_1,
PORT_IN_PS2_0,
PORT_IN_PS2_1
};
enum
{
PORT_OUT_UART0 = 0,
PORT_OUT_UART1,
PORT_OUT_VGA
};
#define CALC_BAUD(b) \
((uint32_t)((float)CPU_FREQ_HZ/(16*b) + 0.5f) - 1);
@@ -74,97 +53,6 @@ void UART1_setbaud(uint32_t baudrate)
*((uint32_t*)SYS_UART1_BAUD) = CALC_BAUD(baudrate);
}
uint32_t GetPort(int file)
{
if (file == 0) // STDIN
return PORT_IN_UART_0;
if (file == 1) // STDOUT
return PORT_OUT_UART0;
if (file == 2) // STDERR
return PORT_OUT_VGA;
if (file == 10) // AUX0
return PORT_IN_UART_1;
if (file == 11) // AUX1
return PORT_OUT_UART1;
}
enum
{
IF_TYPE_INVALID = 0,
IF_TYPE_UART,
IF_TYPE_PS2,
IF_TYPE_VGA
};
typedef struct _suart_if_t
{
volatile uint32_t *pCTRL;
volatile uint32_t *pDATA;
volatile uint32_t *pBAUD;
} uart_if_t;
typedef struct _sps2_if_t
{
volatile uint32_t *pCTRL;
volatile uint32_t *pDATA;
} ps2_if_t;
typedef struct _svga_if_t
{
volatile uint32_t *pCTRL;
volatile uint32_t *pDATA;
volatile uint32_t *pCSRX;
volatile uint32_t *pCSRY;
} vga_if_t;
typedef struct _scon_if_in_t
{
uint32_t type;
void *pIF;
} con_if_in_t;
typedef struct _scon_if_out_t
{
uint32_t type;
void *pIF;
} con_if_out_t;
// ---------------------------------------------------------------------------------
// Globals
// ---------------------------------------------------------------------------------
static uart_if_t uart_if[2] =
{
{(uint32_t*)SYS_UART0_STAT, (uint32_t*)SYS_UART0_DATA, (uint32_t*)SYS_UART0_BAUD},
{(uint32_t*)SYS_UART1_STAT, (uint32_t*)SYS_UART1_DATA, (uint32_t*)SYS_UART1_BAUD}
};
extern ps2_if_t ps2_if[];
static vga_if_t vga_if =
{
(uint32_t*)SYS_VGA_CTRL, (uint32_t*)SYS_VGA_ASCII, (uint32_t*)SYS_VGA_POSX, (uint32_t*)SYS_VGA_POSY
};
static con_if_in_t con_if_in[4] =
{
{IF_TYPE_UART, &uart_if[0]},
{IF_TYPE_UART, &uart_if[1]},
{IF_TYPE_PS2, &ps2_if[0]},
{IF_TYPE_PS2, &ps2_if[1]}
};
static con_if_out_t con_if_out[4] =
{
{IF_TYPE_UART, &uart_if[0]},
{IF_TYPE_UART, &uart_if[1]},
{IF_TYPE_VGA, &vga_if},
{IF_TYPE_INVALID, NULL}
};
// ---------------------------------------------------------------------------------
// MIPS specific
// ---------------------------------------------------------------------------------
@@ -308,495 +196,6 @@ void DCACHE_invalidate_at(uint32_t* pPtr)
);
}
// -------------------------------------------------------------
// BEGIN QUICK AND DIRTY
// -------------------------------------------------------------
//keymap for the AT keyboard SG layout
typedef struct
{
uint8_t scancode;
uint8_t unshifted;
uint8_t shifted;
uint8_t altered;
} libsys_key_entry_t;
#define SHIFTED (1<<0)
#define ALTERED (1<<1)
#define CONTROLLED (1<<2)
static const libsys_key_entry_t __keymap_german[] =
{
{0x1c, 'a', 'A', 'a'},
{0x32, 'b', 'B', 'b'},
{0x21, 'c', 'C', 'c'},
{0x23, 'd', 'D', 'd'},
{0x24, 'e', 'E', ''},
{0x2b, 'f', 'F', 'f'},
{0x34, 'g', 'G', 'g'},
{0x33, 'h', 'H', 'h'},
{0x43, 'i', 'I', 'i'},
{0x3b, 'j', 'J', 'j'},
{0x42, 'k', 'K', 'k'},
{0x4b, 'l', 'L', 'l'},
{0x3a, 'm', 'M', 'µ'},
{0x31, 'n', 'N', 'n'},
{0x44, 'o', 'O', 'o'},
{0x4d, 'p', 'P', 'p'},
{0x15, 'q', 'Q', '@'},
{0x2d, 'r', 'R', 'r'},
{0x1b, 's', 'S', 's'},
{0x2c, 't', 'T', 't'},
{0x3c, 'u', 'U', 'u'},
{0x2a, 'v', 'V', 'v'},
{0x1d, 'w', 'W', 'w'},
{0x22, 'x', 'X', 'x'},
{0x1a, 'y', 'Y', 'y'},
{0x35, 'z', 'Z', 'z'},
{0x16, '1', '!', '1'},
{0x1e, '2', '"', '2'},
{0x26, '3', '§', '3'},
{0x25, '4', '$', '4'},
{0x2e, '5', '%', '5'},
{0x36, '6', '&', '6'},
{0x3d, '7', '/', '{'},
{0x3e, '8', '(', '['},
{0x46, '9', ')', ']'},
{0x45, '0', '=', '}'},
{0x4e, 'ß', '?', '\\'},
{0x29, ' ', ' ', ' '},
{0x41, ',', ';', ','},
{0x49, '.', ':', '.'},
{0x4a, '-', '_', '-'},
{0x52, 'ä', 'Ä', 'ä'},
{0x4c, 'ö', 'Ö', 'ö'},
{0x54, 'ü', 'Ü', 'ü'},
{0x5a, '\n', '\n', '\n'}, //RETURN
{0x5b, '+', '*', '~'},
{0x5d, '#', '\'', '#'},
{0x0e, '^', '°', '^'},
{0x61, '<', '>', '|'},
{0x66, '\b', '\b', '\b'},
{0} //sentinel
};
static char __findkey(char scancode, int mode)
{
const libsys_key_entry_t *p = __keymap_german;
while (p->scancode)
{
if (p->scancode == scancode)
{
switch (mode)
{
case SHIFTED:
return p->shifted;
case ALTERED:
return p->altered;
case ALTERED|SHIFTED:
default:
return p->unshifted;
}
}
p++;
}
return 0;
}
#define RELEASE 0x8000
#define SPECIALKEY 0x4000
#define EXTENDED 0x2000
#define X_CONTROLLED 0x0400
#define X_ALTERED 0x0200
#define X_SHIFTED 0x0100
#define CAPS (SPECIALKEY|0x58)
#define F1 (SPECIALKEY|0x05)
#define F2 (SPECIALKEY|0x06)
#define F3 (SPECIALKEY|0x04)
#define F4 (SPECIALKEY|0x0c)
#define F5 (SPECIALKEY|0x03)
#define F6 (SPECIALKEY|0x0b)
#define F7 (SPECIALKEY|0x83)
#define F8 (SPECIALKEY|0x0a)
#define F9 (SPECIALKEY|0x01)
#define F10 (SPECIALKEY|0x09)
#define F11 (SPECIALKEY|0x78)
#define F12 (SPECIALKEY|0x07)
#define ARROW_UP (SPECIALKEY|EXTENDED|0x75)
#define ARROW_DOWN (SPECIALKEY|EXTENDED|0x72)
#define ARROW_LEFT (SPECIALKEY|EXTENDED|0x6b)
#define ARROW_RIGHT (SPECIALKEY|EXTENDED|0x74)
uint32_t PS2_readchar(ps2_if_t *pIF)
{
if (!pIF)
return -1;
while (!(SYS_PS2_BIT_RX_AVAIL & *pIF->pCTRL));
return (*pIF->pDATA & 0xFF);
}
uint32_t con_readchar(ps2_if_t *pIF)
{
uint8_t c;
static char mode = 0;
static char nextrelease = 0;
static char extended = 0;
int flags = 0;
int char_valid = 0;
uint8_t key;
key = PS2_readchar(pIF);
switch (key)
{
case 0xf0: //release key
nextrelease = 1;
break;
case 0x14: //control key
if (nextrelease)
{
mode &= ~CONTROLLED;
} else
{
mode |= CONTROLLED;
}
nextrelease = 0;
break;
case 0x12: //left SHIFT
case 0x59: //right SHIFT
if (nextrelease)
{
mode &= ~SHIFTED;
} else
{
mode |= SHIFTED;
}
nextrelease = 0;
break;
case 0x11: //left and right ALT (right one also with E0)
if (nextrelease)
{
mode &= ~ALTERED;
} else
{
mode |= ALTERED;
}
nextrelease = 0;
break;
case 0xe0: //extended key following
extended = 1;
break;
default:
if (nextrelease) flags |= RELEASE;
if (extended) flags |= EXTENDED;
flags |= mode << 8;
c = __findkey(key, mode);
char_valid = (c && ((flags & RELEASE) != RELEASE));
nextrelease = 0;
extended = 0;
break;
}
if (char_valid)
{
// fprintf(stderr, "c = %c (%d), flags = 0x%08X, mode = 0x%08X\n", c, c, flags, mode);
return c;
}
return -1;
}
// -------------------------------------------------------------
// END QUICK AND DIRTY
// -------------------------------------------------------------
// ------------------------------------
// Low-level I/O
// ------------------------------------
int UART_readchar(uart_if_t *pIF)
{
if (!pIF)
return -1;
if (SYS_UART_BIT_RX_AVAIL & *pIF->pCTRL)
return (*pIF->pDATA & 0xFF);
return -1;
}
void UART_writechar(uart_if_t *pIF, char c)
{
if (!pIF)
return;
while((SYS_UART_BIT_TX_HALFFULL & *pIF->pCTRL) != 0);
*pIF->pDATA = (uint32_t)c;
}
void cg_writechar(vga_if_t *pIF, char c)
{
uint32_t code;
if (!pIF)
return;
// Output translation
switch(c)
{
case 'Ä':
code = 196;
break;
case 'ä':
code = 228;
break;
case 'Ö':
code = 214;
break;
case 'ö':
code = 246;
break;
case 'Ü':
code = 220;
break;
case 'ü':
code = 252;
break;
case '&':
code = 230;
break;
case '§':
code = 167;
break;
case 'ß':
code = 223;
break;
case '°':
code = 176;
break;
case 'µ':
code = 181;
break;
default:
code = (uint32_t)c;
break;
}
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pDATA = code;
}
void cg_set_csr_x(vga_if_t *pIF, uint32_t coord)
{
if (!pIF)
return;
// Set cursor
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCSRX = coord;
}
void cg_set_csr_y(vga_if_t *pIF, uint32_t coord)
{
if (!pIF)
return;
// Set cursor
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCSRY = coord;
}
void cg_clr_line(vga_if_t *pIF)
{
if (!pIF)
return;
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCTRL |= SYS_VGA_BIT_CLRLINE;
}
void cg_clr_screen(vga_if_t *pIF)
{
if (!pIF)
return;
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCTRL |= SYS_VGA_BIT_CLRSCR;
// Cursor home
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCSRX = 0;
while (!(*pIF->pCTRL & SYS_VGA_BIT_CGRDY));
*pIF->pCSRY = 0;
}
void Screen_clr(void)
{
cg_clr_screen(&vga_if);
}
void Screen_line_clr(void)
{
cg_clr_line(&vga_if);
}
void Screen_csr_set_x(uint32_t coord)
{
cg_set_csr_x(&vga_if, coord);
}
void Screen_csr_set_y(uint32_t coord)
{
cg_set_csr_y(&vga_if, coord);
}
uint32_t Screen_get_resx(void)
{
uint32_t volatile *pVGA_res = (uint32_t*)SYS_VGA_RES;
return (0xFFF & (*pVGA_res >> 0));
}
uint32_t Screen_get_resy(void)
{
uint32_t volatile *pVGA_res = (uint32_t*)SYS_VGA_RES;
return (0xFFF & (*pVGA_res >> 12));
}
uint32_t Screen_get_fps(void)
{
uint32_t volatile *pVGA_res = (uint32_t*)SYS_VGA_RES;
return (0xFF & (*pVGA_res >> 24));
}
// ---------------------------------------------------------------------------------
void flush(uint32_t port)
{
int c;
con_if_in_t *pIF;
pIF = &con_if_in[port];
do
{
switch(pIF->type)
{
case IF_TYPE_UART:
c = UART_readchar((uart_if_t*)pIF->pIF);
break;
case IF_TYPE_PS2:
c = con_readchar((ps2_if_t*)pIF->pIF);
break;
default:
break;
}
} while(c > 0);
}
char readchar(uint32_t port)
{
int c;
con_if_in_t *pIF;
pIF = &con_if_in[port];
do
{
switch(pIF->type)
{
case IF_TYPE_UART:
c = UART_readchar((uart_if_t*)pIF->pIF);
break;
case IF_TYPE_PS2:
c = con_readchar((ps2_if_t*)pIF->pIF);
break;
default:
break;
}
} while(c < 0);
return (char)c;
}
void writechar(uint32_t port, char c)
{
con_if_out_t *pIF;
pIF = &con_if_out[port];
switch(pIF->type)
{
case IF_TYPE_UART:
UART_writechar((uart_if_t*)pIF->pIF, c);
break;
case IF_TYPE_VGA:
cg_writechar((vga_if_t*)pIF->pIF, c);
break;
default:
break;
}
}
void _putchar(uint32_t port, char c)
{
con_if_out_t *pIF;
pIF = &con_if_out[port];
switch(pIF->type)
{
case IF_TYPE_UART:
if (c == 0x0A)
UART_writechar((uart_if_t*)pIF->pIF, 0x0D);
UART_writechar((uart_if_t*)pIF->pIF, c);
break;
case IF_TYPE_VGA:
cg_writechar((vga_if_t*)pIF->pIF, c);
if (c == 0x0A)
{
cg_clr_line((vga_if_t*)pIF->pIF);
cg_writechar((vga_if_t*)pIF->pIF, 0x0D);
}
break;
default:
break;
}
}
char _getchar(void)
{
return readchar(GetPort(0));
}
void _exit (int exitcode)
{
fflush(stdout);
@@ -951,44 +350,39 @@ int lseek(int file, int ptr, int dir)
int open(const char *name, int flags, int mode)
{
// sputs("open (\""); sputs(name); sputs("\")\n");
errno = EIO;
return -1;
return con_open(name);
}
int close(int file)
{
// sputs("close ("); print_word(file); sputs(")\n");
return 0;
return con_close(file);
}
int read(int file, char *ptr, int len)
{
uint32_t port;
int i;
char c;
// sputs("read (file="); print_word(file); sputs(", len ="); print_word(len); sputs(")\n");
i = 0;
port = GetPort(file);
while (i < len)
{
c = readchar(port);
c = readchar(file);
if (c == 0x04)
break;
if ((c == 0x0D) || (c == 0x0A))
{
writechar(port, 0x0D);
writechar(file, 0x0D);
ptr[i++] = 0x0D;
writechar(port, 0x0A);
writechar(file, 0x0A);
ptr[i++] = 0x0A;
break;
}
writechar(port, c);
writechar(file, c);
ptr[i++] = c;
}
@@ -997,14 +391,12 @@ int read(int file, char *ptr, int len)
int write(int file, char *ptr, int len)
{
uint32_t port;
int i = 0;
// sputs("write ("); print_word(file); sputs(")\n");
port = GetPort(file);
for (i=0; i < len; i++)
_putchar(port, *ptr++);
_putchar(file, *ptr++);
return i;
}
@@ -1052,7 +444,7 @@ int sputs(char const *pStr)
char const *start = pStr;
while(*pStr)
_putchar(GetPort(1), *(pStr++));
_putchar(1, *(pStr++));
return pStr - start;
}
@@ -1072,7 +464,7 @@ void print_byte(char byte)
else
c = nibble + 'A' - 10;
_putchar(GetPort(1), c);
_putchar(1, c);
}
}
@@ -1145,7 +537,7 @@ void memdump(uint8_t *pBuf, int print_offset_only, int num_bytes_per_row, int le
if((c < 0x20) || (c > 0x7F))
c = '.';
_putchar(GetPort(1), c);
_putchar(1, c);
cnt_asc--;
}
else