Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.o
*.d
main
attach_gdb_to_this
main
Expand Down
31 changes: 15 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ OBJCOPY=m68k-elf-objcopy
QEMU=/opt/m68k/bin/qemu-system-m68k

CFLAGS=-nostdlib -nostartfiles -nodefaultlibs
CFLAGS+=-Wall -Werror
CFLAGS+=-m68000
CFLAGS+=-Os

main: assembly.o iv.o screen.o main.o exception.o linker.x kmem.o string.o
${LD} main.o assembly.o iv.o screen.o exception.o kmem.o string.o -o main -T linker.x -Map main.map
SOURCES=assembly.c screen.c main.c exception.c kmem.c string.c
OBJECTS=$(subst .c,.o,$(SOURCES))

main: ${OBJECTS} iv.o linker.x
${LD} ${OBJECTS} iv.o -o main -T linker.x -Map main.map
cp main attach_gdb_to_this
${OBJCOPY} -O srec main

Expand All @@ -22,20 +26,15 @@ disassemble: main
iv.o: iv.asm
${AS} -o iv.o iv.asm

main.o: main.c
${CC} ${CFLAGS} -c main.c -o main.o

assembly.o: assembly.h assembly.c
${CC} ${CFLAGS} -o assembly.o -c assembly.c

screen.o: screen.h screen.c assembly.h
${CC} ${CFLAGS} -o screen.o -c screen.c

exception.o: exception.c
${CC} ${CFLAGS} -o exception.o -c exception.c
%.o: %.c
${CC} ${CFLAGS} -o $@ -c $<
%.d: %.c
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

kmem.o: kmem.h kmem.c
${CC} -o kmem.o -c kmem.c
DEPENDS=$(subst .c,.d,$(SOURCES))
-include ${DEPENDS}

run: main
${QEMU} -M cecs -nographic -kernel main -gdb tcp::1234
Expand All @@ -44,5 +43,5 @@ debug: main
${QEMU} -M cecs -nographic -kernel main -S -gdb tcp::1234

clean:
rm assembly.o screen.o iv.o attach_gdb_to_this main main.o kmem.o main.map string.o
rm iv.o attach_gdb_to_this main main.map ${DEPENDS} ${OBJECTS}

5 changes: 1 addition & 4 deletions iv.asm
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.data
.org 0x0
dc.l __asm_stack__,main
.equ __asm_stack__,0x4400
.section .text
dc.l __asm_stack__,main
dc.l __asm_stack__,entry
dc.l excep_undef
dc.l excep_undef
/* 4 */
Expand Down
4 changes: 2 additions & 2 deletions kmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ char *blocks;

void kfree(void *ptr) {
ptr = ptr - 4;
int size = *((int *)ptr);
int __attribute__ ((unused)) size = *((int *)ptr);
int index = (((int) ptr) - MEM_START)/MEM_BLOCK_SIZE;
CLRBIT(index);
*((int *)ptr) = 0;
Expand Down Expand Up @@ -53,7 +53,7 @@ void *kmalloc(int size) {
void kmeminit() {
int needed_blocks = (MEM_BITSET_BYTES+MEM_BLOCK_SIZE-1)/MEM_BLOCK_SIZE;
int i;
blocks = (int *)MEM_START;
blocks = (char *)MEM_START;
for (i = 0; i < needed_blocks; i++)
SETBIT(i);
for (; i < MEM_BITSET_BITS; i++)
Expand Down
8 changes: 6 additions & 2 deletions linker.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ SECTIONS {
.text : {
* (.text*);
* (.rodata*);
etext = .; /*End of test section*/
} >ROM
/* The C code needs to reference all initialized data at its RAM address,
but we want it to be writable. This causes the .data section to
be physically located after the .text section in ROM, but referenced
at its RAM address. Code must copy this data section to RAM at boot.
*/
.data : AT (etext) { * (.data); } >RAM
.data : {
data_load = LOADADDR(.data);
data_start = .;
* (.data);
data_end = .;
} >RAM AT>ROM
}
31 changes: 28 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
#include "assembly.h"
#include "screen.h"
#include "kmem.h"
#include "string.h"

extern unsigned char data_load;
extern unsigned char data_start;
extern unsigned char data_end;

int foo = 0xdeadbeef;

/* Copy the .data section from ROM to RAM */
static void copy_data(void)
{
unsigned char *rom_data = &data_load;
unsigned char *ram_data = &data_start;

if (foo == 0xdeadbeef)
putstr("Warning: .data appears initialized before copying\n");

/* Could be replaced with a call to memcpy */
while (ram_data < &data_end) {
*(ram_data++) = *(rom_data++);
}
if (foo != 0xdeadbeef)
putstr("Warning: .data not initialized as expected\n");
}

void *test_malloc(int bytes) {
void *ptr = kmalloc(bytes);
Expand All @@ -19,15 +43,16 @@ void test_free(void *ptr) {

void memtest() {
void *p1 = test_malloc(124);
void *p2 = test_malloc(124);
void __attribute__ ((unused)) *p2 = test_malloc(124);
test_free(p1);
void *p3 = test_malloc(124);
void __attribute__ ((unused)) *p3 = test_malloc(124);

}

void shell() {
char ptr[64];

copy_data();
putstr("Welcome to the (unstable) C Kernel\n");

while(1)
Expand Down Expand Up @@ -57,7 +82,7 @@ void shell() {
}
}

void main() {
void entry() {
__asm_initialize__();
kmeminit();

Expand Down