Browse Source

Way too many changes to list

pull/2/head
Stephen 8 months ago
parent
commit
079b750393
8 changed files with 152 additions and 51 deletions
  1. +12
    -12
      os/Makefile
  2. +1
    -5
      os/drivers/serial.c
  3. +5
    -5
      os/libc/stdlib.c
  4. +20
    -10
      os/libc/string.c
  5. +1
    -8
      os/main.c
  6. +43
    -10
      os/prog/tty.c
  7. +0
    -1
      os/start.s
  8. +70
    -0
      os/tests/main.c

+ 12
- 12
os/Makefile View File

@ -1,26 +1,26 @@
C_SOURCES = $(wildcard drivers/*.c cpu/*.c libc/*.c prog/*.c )
S_SOURCES = $(wildcard cpu/lib/*.s)
C_SOURCES = $(wildcard drivers/*.c cpu/*.c libc/*.c prog/*.c)
HEADERS = $(wildcard drivers/*.h cpu/*.h libc/*.h prog/*.h)
OBJ = ${C_SOURCES:.c=.o}
OBJ = ${C_SOURCES:.c=.o} ${S_SOURCES:.s=.o}
os.bin: start.o main.o ${OBJ} Linker.ld
m68k-elf-ld -T Linker.ld start.o main.o ${OBJ} -o os.bin -O2 -nostdlib
m68k-elf-gcc -T Linker.ld start.o main.o ${OBJ} -o os.bin -O2 -nostdlib -lgcc -mcpu=68000
start.o: start.s
m68k-elf-as -mcpu=68000 -o start.o start.s
%.o: %.s
m68k-elf-as -mcpu=68000 -o $@ $<
%.o: %.c %.h
m68k-elf-gcc -c $< -o $@ -ffreestanding -O2 -Wall -Wextra
m68k-elf-gcc -mcpu=68000 -c $< -o $@ -ffreestanding -O2 -Wall -Wextra -nostdlib -lgcc
%.o: %.cpp %.h
m68k-elf-g++ -c $< -o $@ -ffreestanding -O2 -Wall -Wextra
m68k-elf-g++ -mcpu=68000 -c $< -o $@ -ffreestanding -O2 -Wall -Wextra -nostdlib -lgcc
TESTS = tests/main.c libc/string.c
test: ${TESTS}
gcc ${TESTS} -g -o test
test: ${TESTS} libc/stdlib.c
gcc ${TESTS} libc/stdlib.c -g -o test
clean:
rm -rf *.bin *.dis *.o *.elf
rm -rf drivers/*.o cpu/*.o libc/*.o prog/*.o
rm test
rm -rf ${OBJ}
rm -f test

+ 1
- 5
os/drivers/serial.c View File

@ -8,8 +8,6 @@ volatile char *const SERIAL_BASE = (char*)0xF30001;
//it points @ MR1A
//The second time, it points @MR2A
//we don't want the compiler to optimize this
#pragma GCC push_options
#pragma GCC optimize("O0")
void serial_init()
{
*SERIAL_BASE = 0x3; //MR1A
@ -59,7 +57,7 @@ int serial_readline(char *buffer, int port)
int serial_putchar(int port, char c)
{
//Only port A implemented for now;
//Only port A implemented for now
if(port != 0) return 0;
//Infinite loop until TxRDY is set
while((*(SERIAL_BASE + 2) & 4) == 0) {}
@ -96,5 +94,3 @@ char serial_readA()
// TODO take port into account
return (file_t){ .write = serial_writeA, .read = serial_readA };
}*/
#pragma GCC pop_options

+ 5
- 5
os/libc/stdlib.c View File

@ -9,8 +9,8 @@ int isspace(int ch)
(ch == ' ');
}
//TODO this should set errors if it can't parse
long long strtol(const char *str, char **str_end, int base)
// TODO this should set errors if it can't parse
unsigned long long strtol(const char *str, char **str_end, unsigned int base)
{
//get rid of whitespace
int i = 0;
@ -43,13 +43,13 @@ long long strtol(const char *str, char **str_end, int base)
}
}
long long output = 0;
unsigned int output = 0; // TODO fix this
//base 10 only - temp
int c = str[i];
unsigned char c = str[i];
if(c >= 'a') c -= 0x20; //lowercase
if(c >= '9') c -= 0x37; //to int
else c -= '0';
while(c >= 0 && c < base)
while(c < base && str[i] != 0)
{
output *= base;
output += c;


+ 20
- 10
os/libc/string.c View File

@ -1,3 +1,5 @@
#include <stdarg.h>
#include "stddef.h"
@ -62,8 +64,11 @@ char *strtok(char *str, const char *delim)
return str;
}
int xtostr(char *buffer, long long value, int capitalize)
int xtostr(char *buffer, unsigned long long value, int capitalize)
{
*buffer++ = '0';
*buffer++ = 'x';
char *ogBuffer = buffer;
char offset = capitalize ? 'A' : 'a';
//Used so we don't print leading 0s
@ -80,17 +85,17 @@ int xtostr(char *buffer, long long value, int capitalize)
}
}
*buffer = 0;
return ogBuffer - buffer;
return 2 + buffer - ogBuffer;
}
int sprintf(char *buffer, const char *format, ...)
{
va_list args;
va_start(args, format);
char *ogBuffer = buffer;
//variable args start 1 byte below format
//and continue down
char *vaArgs = (char *)&format - 1;
long offset;
unsigned int offset;
while(*format != 0)
{
@ -103,15 +108,18 @@ int sprintf(char *buffer, const char *format, ...)
//into the buffer
//then, increment the buffer
//and decrement vaArgs
*(buffer++) = *(vaArgs--);
*(buffer++) = va_arg(args, int);
format++;
break;
case 'x':
offset = xtostr(buffer, *(vaArgs--), 0);
offset = xtostr(buffer, va_arg(args, unsigned long long), 0);
buffer += offset;
format++;
break;
case 'X':
offset = xtostr(buffer, *(vaArgs--), 1);
offset = xtostr(buffer, va_arg(args, unsigned long long), 1);
buffer += offset;
format++;
break;
default:
*(buffer++) = '%';
@ -123,6 +131,8 @@ int sprintf(char *buffer, const char *format, ...)
}
format++;
}
va_end(args);
*buffer = 0; //null terminate
return ogBuffer - buffer;
return buffer - ogBuffer;
}

+ 1
- 8
os/main.c View File

@ -7,12 +7,7 @@ void kmain()
serial_init();
serial_set_baud(BAUD9600, 0);
serial_putchar(0, 'h');
while(1) {}
//char c = serial_getchar(0);
//serial_putchar(0, c);
serial_print(0, "\r\n");
serial_print(0, "===================================================================================================\r\n");
serial_print(0, "\\ \\ / /_ _| \\ | | __ \\| ____| | / __ \\ \\ / / ____| __ \\ / __ \\ / ____|\r\n");
serial_print(0, " \\ \\ /\\ / / | | | \\| | | | | |__ | | | | | \\ \\ /\\ / /| |__ | |__) | | | | | (___\r\n");
@ -21,8 +16,6 @@ void kmain()
serial_print(0, " \\/ \\/ |_____|_| \\_|_____/|_| |______\\____/ \\/ \\/ |______|_| \\_\\ \\____/|_____/\r\n");
serial_print(0, "===================================================================================================\r\n");
serial_print(0, "www.scd31.com\r\n");
while(1) {}
//keyboard_init();
tty_init();


+ 43
- 10
os/prog/tty.c View File

@ -24,9 +24,8 @@ void tty_init()
while(1)
{
tty_readline(buffer, BUFFER_LEN);
tty_print("0");
tty_process(buffer);
tty_print("\r\n> ");
tty_print("> ");
}
}
@ -39,6 +38,8 @@ void tty_readline(char *buf, long len)
buf[i++] = c;
c = tty_getchar();
}
tty_print("\r\n");
buf[i] = 0;
}
char tty_getchar()
@ -58,15 +59,20 @@ void tty_putchar(char c)
serial_putchar(0, c);
}
#pragma GCC push_options
#pragma GCC optimize("O0")
void tty_process(char *buf)
{
char *cmd = strtok(buf, " ");
tty_print("We got: ");
tty_print(cmd);
tty_print("\r\n");
//TODO case insensitive
if(strcmp(cmd, "help"))
if(strcmp(cmd, "help") == 0)
{
tty_print("Not implemented\r\n");
}
else if(strcmp(cmd, "peek"))
else if(strcmp(cmd, "peek") == 0)
{
//Print value @ address
char *arg = strtok(NULL, " ");
@ -75,12 +81,38 @@ void tty_process(char *buf)
tty_print("Usage: peek ADDR\r\n");
return;
}
char *ptr = (char *)(long)strtol(arg, NULL, 0);
char buffer[15];
sprintf(buffer, "%X\r\n", *ptr);
tty_print("Arg: ");
tty_print(arg);
tty_print("\r\n");
char buffer[100];
//sprintf(buffer, "Hello! %X\r\n", 0xDEADB33F);
//tty_print(buffer);
//long long a = 0xD;
//long long b = 0x13BEEF;
//sprintf(buffer, "abcdefg: %X %X %X\r\n", a, b, a * b);
//if(a * b == 0)
//{
// tty_print("Yes, it is 0");
//}
//tty_print(buffer);
unsigned char *ptr = (unsigned char *)(unsigned long)strtol(arg, NULL, 0);
if(ptr == 0xB33F)
{
tty_print("It is 0xB33F\r\n");
}
sprintf(buffer, "%X\r\n", (unsigned long long) *ptr);
tty_print(buffer);
}
else if(strcmp(cmd, "poke"))
else if(strcmp(cmd, "poke") == 0)
{
char *addrStr = strtok(NULL, " ");
char *valStr = strtok(NULL, " ");
@ -89,7 +121,7 @@ void tty_process(char *buf)
tty_print("Usage: poke ADDR NEWVAL\r\n");
return;
}
long long valL = strtol(valStr, NULL, 0);
unsigned long long valL = strtol(valStr, NULL, 0);
if(valL > 255)
{
tty_print("WARNING: value is more than 1 byte and therefore will be truncated\r\n");
@ -99,6 +131,7 @@ void tty_process(char *buf)
}
else
{
tty_print("Unknown command. Type help for help.");
tty_print("Unknown command. Type help for help.\r\n");
}
}
#pragma GCC pop_options

+ 0
- 1
os/start.s View File

@ -38,7 +38,6 @@
.endm
.long 0x17FFFE /* SP at top of 256K of RAM - but it has to be an even address */
/* TODO we should be able to set it to 0x180000 */
.long _start
.org 0x100


+ 70
- 0
os/tests/main.c View File

@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdint.h>
#include "../libc/string.h"
#include "../libc/stdlib.h"
/*
This file is a mega-hack
@ -11,12 +13,35 @@
int totalTests = 0;
int failedTests = 0;
//copied & pasted from OS(which isn't great)
// modified for little endian
typedef struct
{
uint16_t upper;
int16_t lower;
} IntBytes;
typedef union
{
IntBytes words;
int64_t num;
} IntUnion;
// Return a * b
int64_t muldi3(IntUnion a, IntUnion b)
{
return ((a.words.lower * b.num) << 16) +
((a.words.upper * b.num));
}
//end
void assert_long(long a, long b)
{
totalTests++;
if(a != b)
{
failedTests++;
printf("ERROR: expected '%d' to be '%d'\r\n", b, a);
}
}
@ -62,12 +87,57 @@ void test_strtok()
assert_str("sample string.", ret);
ret = strtok(NULL, ",/+-");
assert_ptr(NULL, ret);
char s2[250] = "This,is+,a-sample string.";
ret = strtok(s2, " ");
assert_str("This,is+,a-sample", ret);
}
void test_strcmp()
{
assert_long(strcmp("a", "b"), -1);
assert_long(strcmp("a", "a"), 0);
}
void test_strtol()
{
assert_long(123, strtol("123", 0, 0));
assert_long(0x456, strtol("0x456", 0, 0));
}
void test_muldi3()
{
assert_long(100, muldi3((IntUnion){.num=10}, (IntUnion){.num=10}));
assert_long(0x279E34B4, muldi3((IntUnion){.num=0x1321}, (IntUnion){.num=0x21234}));
assert_long(-427009572, muldi3((IntUnion){.num=-20938}, (IntUnion){.num=20394}));
}
void test_sprintf()
{
char buffer[250];
assert_long(21, sprintf(buffer, "Hello %x %X %c", 0x1A2B, 0x3C4D, 'z'));
assert_str("Hello 0x1a2b 0x3C4D z", buffer);
assert_long(2, sprintf(buffer, "%X", 0));
assert_str("0x", buffer);
assert_long(18, sprintf(buffer, "Hello %x world", 0x1234));
assert_str(buffer, "Hello 0x1234 world");
assert_long(9, sprintf(buffer, "%X", 0x1234567));
assert_str(buffer, "0x1234567");
}
int main(int argc, char **argv)
{
//run tests
test_strtok();
test_strcmp();
test_strtol();
test_muldi3();
test_sprintf();
//print info
printf("Ran %d tests.\r\n", totalTests);
printf("%d failures\r\n", failedTests);


Loading…
Cancel
Save