2016-05-24 07:27:26 +00:00
|
|
|
/* Copyright 2016 secXsQuared
|
|
|
|
* Distributed under GPL license
|
|
|
|
* See COPYING under root for details
|
|
|
|
*/
|
|
|
|
|
2018-01-25 19:11:22 +00:00
|
|
|
#include "abi.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "lib/sxtdlib.h"
|
2017-02-23 02:36:00 +00:00
|
|
|
#include "print.h"
|
2016-07-09 03:01:33 +00:00
|
|
|
|
2018-01-25 09:53:35 +00:00
|
|
|
#define get_column(pos) (pos % 80)
|
|
|
|
#define get_row(pos) (pos / 80)
|
|
|
|
#define get_pos(row,col) ((row) * 80 + (col))
|
|
|
|
|
2016-07-09 03:01:33 +00:00
|
|
|
static uint64_t text_pos;
|
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI hal_print_init()
|
2016-07-09 03:01:33 +00:00
|
|
|
{
|
|
|
|
text_pos = 0;
|
|
|
|
}
|
2015-02-01 22:14:44 +00:00
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI halp_print_scroll()
|
2015-02-01 22:14:44 +00:00
|
|
|
{
|
2017-02-01 03:26:08 +00:00
|
|
|
lb_mem_move((void *) (0xb8000 + get_pos(1, 0) * 2), (void *) (0xb8000 + get_pos(0, 0) * 2), (80 * 24) * 2);
|
2015-02-01 22:14:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI halp_print_str(char const *str)
|
2015-02-01 22:14:44 +00:00
|
|
|
{
|
|
|
|
if(str == NULL)
|
|
|
|
return;
|
|
|
|
while (*str != 0)
|
|
|
|
{
|
|
|
|
if(*str == '\n')
|
|
|
|
{
|
|
|
|
text_pos = 80 * (get_row(text_pos) + 1);
|
|
|
|
if(text_pos > 80 * 25 - 1)
|
|
|
|
{
|
|
|
|
//can't hold
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_scroll();
|
2017-02-01 03:26:08 +00:00
|
|
|
lb_mem_set((void *) (0xb8000 + 80 * 24 * 2), 0, 80 * 2); // clear last row
|
2015-02-01 22:14:44 +00:00
|
|
|
text_pos = 80 * 24;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (text_pos > 80 * 25 - 1)
|
|
|
|
{
|
|
|
|
//can't hold
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_scroll();
|
2015-02-01 22:14:44 +00:00
|
|
|
text_pos = 80 * 24;
|
|
|
|
}
|
|
|
|
*((char*)(0xb8000) + text_pos*2) = *str;
|
|
|
|
*((char*)(0xb8000) + text_pos*2 + 1) = 7;
|
|
|
|
str++;
|
|
|
|
text_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI halp_print_uint(uint64_t number)
|
2015-02-07 12:10:50 +00:00
|
|
|
{
|
|
|
|
char arr[21]; // do not need to initialize
|
|
|
|
arr[20] = 0; //zero-terminated
|
|
|
|
uint32_t index = 19;
|
|
|
|
uint32_t const div = 10;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
uint64_t quo = number / div;
|
|
|
|
uint64_t rmd = number % div;
|
|
|
|
number = quo;
|
|
|
|
arr[index--] = (char) ('0' + rmd);
|
|
|
|
if (number == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(&(arr[index + 1]));
|
2015-02-07 12:10:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI halp_print_int(int64_t number)
|
2015-02-01 22:14:44 +00:00
|
|
|
{
|
|
|
|
char arr[21]; // do not need to initialize
|
|
|
|
arr[20] = 0; //zero-terminated
|
|
|
|
uint32_t index = 19;
|
|
|
|
uint32_t isNegative = 0;
|
|
|
|
uint32_t const div = 10;
|
|
|
|
if (number < 0)
|
|
|
|
{
|
|
|
|
isNegative = 1;
|
|
|
|
number *= -1;
|
|
|
|
}
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int64_t quo = number / div;
|
|
|
|
int64_t rmd = number % div;
|
|
|
|
number = quo;
|
2015-02-07 12:10:50 +00:00
|
|
|
arr[index--] = (char) ('0' + rmd);
|
2015-02-01 22:14:44 +00:00
|
|
|
if (number == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isNegative)
|
|
|
|
{
|
2015-02-07 12:10:50 +00:00
|
|
|
arr[index--] = '-';
|
2015-02-01 22:14:44 +00:00
|
|
|
}
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(&(arr[index + 1]));
|
2015-02-01 22:14:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:50:35 +00:00
|
|
|
static void KABI halp_print_hex(uint64_t number, uint64_t capital)
|
2015-02-01 22:14:44 +00:00
|
|
|
{
|
2015-02-07 12:10:50 +00:00
|
|
|
char const lookup_table_cap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
char const lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
2015-09-03 15:18:56 +00:00
|
|
|
char const * const look_up = capital == 1 ? &lookup_table_cap[0] : &lookup_table[0];
|
2015-02-08 08:18:22 +00:00
|
|
|
char arr[17];
|
|
|
|
arr[16] = 0; //zero-terminated
|
|
|
|
uint32_t index = 15;
|
2015-02-01 22:14:44 +00:00
|
|
|
uint32_t const div = 16;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
uint64_t quo = number / div;
|
|
|
|
uint64_t rmd = number % div;
|
|
|
|
number = quo;
|
2015-02-07 12:10:50 +00:00
|
|
|
arr[index--] = look_up[rmd];
|
2015-02-01 22:14:44 +00:00
|
|
|
if (number == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(&(arr[index + 1]));
|
2015-02-01 22:14:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-01 03:26:08 +00:00
|
|
|
void KABI hal_clear_screen(void)
|
2015-02-01 22:14:44 +00:00
|
|
|
{
|
|
|
|
text_pos = 0; // reset text_pos
|
2017-02-01 03:26:08 +00:00
|
|
|
lb_mem_set((void *) 0xb8000, 0, 25 * 80 * 2);
|
2015-02-01 22:14:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-07 12:10:50 +00:00
|
|
|
|
2018-01-25 19:11:22 +00:00
|
|
|
void KABI hal_vprintf(char const * format, va_list args)
|
2015-02-07 12:10:50 +00:00
|
|
|
{
|
|
|
|
va_start(args, format);
|
|
|
|
char buf[2] = {0, 0};
|
|
|
|
int32_t d;
|
|
|
|
uint32_t u;
|
|
|
|
char* s;
|
|
|
|
char c;
|
|
|
|
for(;*format != '\0';format++)
|
|
|
|
{
|
|
|
|
if (*format != '%')
|
|
|
|
{
|
|
|
|
buf[0] = *format;
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(buf);
|
2015-02-07 12:10:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
switch (*format)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
d = va_arg(args, int64_t);
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_int(d);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
u = va_arg(args, uint64_t);
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_uint(u);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
s = va_arg(args, char *);
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(s);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
c = va_arg(args, int64_t);
|
|
|
|
buf[0] = c;
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(buf);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
u = va_arg(args, uint64_t);
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_hex(u, 0);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
u = va_arg(args, uint64_t);
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_hex(u, 1);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
buf[0] = '%';
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(buf);
|
2015-02-07 12:10:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[0] = '%';
|
2017-03-10 02:50:35 +00:00
|
|
|
halp_print_str(buf);
|
2015-02-07 12:10:50 +00:00
|
|
|
format--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 19:11:22 +00:00
|
|
|
|
|
|
|
void KABI hal_printf(char const *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
hal_vprintf(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|