OS Kernel on Dreamcast

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

OS Kernel on Dreamcast

Post by Masen »

Hi

im working on an OS kernel (written in C) and thought about if it would be possible to port it on DC and Booting from a existing Bootloader from LinuxSH ...

so would this be possible and how ?
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

In theory, it's possible. It depends on the design of your OS.

You don't really need a bootloader. The Dreamcast's built-in loader is capable of loading very large binaries. It dumps them in memory at a certain address, and you can execute from there.

You basically need to provide a Dreamcast port of your kernel, and appropriate drivers to make it do something interesting (like a serial port driver). You can probably get the required information (maybe even code) from KallistiOS. Any PC-specific code you have (especially anything in x86 assembly) must be re-written.
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

but how i can compile the kernel to run it on DC ? would it work when i scramble the kernel Bin file ???

it is not realy necassary that the OS will run perfectly with 100% of it functions ... it would be nice if it would boot and display it boot messages and then maybe crashes ... so how i can compile it ? it has a c-kernel with a asm kernel entry so it would need the sh assembler and c cross compiler or ?

but ive tried to get the SH Linux kernel too boot ... but ive failed for several reasons ...
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

You'll need a C compiler, and an assembler. You'd also need to write the appropriate entry code in SH-4 assembly.

How are you building the PC version?
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

with djgpp under windows .... starring gcc as the C Compiler and nasm as the assembler ^^
You'd also need to write the appropriate entry code in SH-4 assembly.

ok ... but how i get started ??? are they any tuts for this matter ?? what about DreamShell are there any english docs ???
User avatar
Morphv2
Insane DCEmu
Insane DCEmu
Posts: 230
Joined: Sun Sep 12, 2004 1:36 pm
Location: Someplace
Has thanked: 0
Been thanked: 0
Contact:

Post by Morphv2 »

dcdev @ yahoo groups has a lot of info about SH4 assembly and such. Thats where most of the KalistiOS was put together (not counting Dan's desk XD). Anything that you would be looking for would be found there. Just dont try and ask anything TRULY hardware related, like modding a console. They dont,know jack squat about that :p
Image
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

You're using a DOS compiler under Windows? Seems strange, but...

You should be able to use the same tools that we use to build KallistiOS and homebrew games. Probably the best way would be to download a Dreamcast cross compiler that uses KallistiOS (I think there are some around here that use Cygwin as a base), then use KallistiOS as a guide (complete source code is available) to getting your own OS working with those tools.
are they any tuts for this matter ??
No, only example code, in the form of KallistiOS, libdream (older version of KallistiOS), NetBSD, Linux, and a few older assembly examples. The SH-4 isn't really used in anything aside from the Dreamcast (which is supposed to be a closed system), and a few older WinCE handhelds (which you could never do system-level programming for anyway).

There's a fair bit of hardware documentation available though. A good site to read through is this one.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7497
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 9 times
Contact:

Post by Quzar »

Don't forget the SH4 is also used in little weird embedded devices that are driven pretty much entirely by the sh versions of linux, QNX and NetBSD such as the following:

http://www.emtrion.com/products/hicosh4.html
http://sh2000.sh-linux.org/sh2004.html
http://www.si-linux.com/product/cat68701/
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

searching in my cygwin dirs ive found this compilers and etc. :

sh4-linux-as / the assembler i think ?
sh4-linux-gcc
sh4-linux-ld
sh4-linux-objcopy and other binutils
;

sh-elf-as
sh-elf-gcc
sh-elf-ld
sh-elf-objcopy and other binutils

;and:

as
gcc
ld
opjcopy and other binutils


so when the as is the assembler (other name for gas or ?) i should have all the tools i need or ???
so to the kernel problem , ive read in the SegaDocs in the Docs for Embedded Systems that you need a entry assembler data crt0 witch calls the main . but how i can merge the kernel entry and the kernel in the linker script ?
and can i compile it to an elf file witch the sh-elf compile can than convert it to an .bin and scramble it to an 1st_read.bin ???
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

sh4-linux-as / the assembler i think ?
sh4-linux-gcc
sh4-linux-ld
sh4-linux-objcopy and other binutils
;

sh-elf-as
sh-elf-gcc
sh-elf-ld
sh-elf-objcopy and other binutils
They're both suitable. The first are targeted at SH-4 Linux, but should work fine. The second are targeted at generic SH and produce ELF format binaries, which will also be suitable. However, you need all the additional compiler and linker flags (the same as you'd be using on the PC side, pretty much) to remove the standard libraries, header files, and so on.

as is indeed the assembler.
as
gcc
ld
opjcopy and other binutils
Native compilers, which generate x86 code.
so to the kernel problem , ive read in the SegaDocs in the Docs for Embedded Systems that you need a entry assembler data crt0 witch calls the main . but how i can merge the kernel entry and the kernel in the linker script ?
Exactly the same way you're doing it on the PC side.

The general process is to make damned sure that your crt0 is being linked in before anything else. Have a look at KallistiOS for a Dreamcast-specific example on how to do that. Doesn't use a linker script (it uses command-line parameters instead), but it's basically the same thing. The kernel entry code should be right at the beginning of the file, because it's the very first piece of code that will be executed by the Dreamcast's bootloader.
and can i compile it to an elf file witch the sh-elf compile can than convert it to an .bin and scramble it to an 1st_read.bin ???
Yes, that's the general procedure. You can also upload the ELF file over a serial cable or broadband adaptor.
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

But from where i get the assembler entry file ? im not so good in assembler and the SH assembly is different ... for my OS im using GRUB as a bootloader and my Entry File doesnt work , ive tried startup.s from KOS and kernel-boot from LinuxSH but non of them work ...
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 5 times

Post by nymus »

Since you're not doing anything too complex in asm, you don't really know how to program in sh4. Most of it should be familiar if you've already coded for the x86.

Unfortunately, there is no "official" asm entry file. Most of the code you need should be in startup.s. This is the file that needs to be at the head of your binary. If you look in the gcc sources in the config/sh directory, you'll see the links gcc needs (init, fini, bss...) which I think are present in the startup.s. (another place to look is in the ronin sources. Ronin is another dreamcast coding library you can look at). Once these links are provided, gcc will be happy, so you'll just tell it that everything you need is available by passing the flags "-nostdlib -nostartfiles" and link with your entry file as the first file.

You will see exception handling code in there too, which you'll have to interface with or rewrite as you see fit.

You can then call your io libs from C or C+asm as usual since, with the exception of the gdrom, dreamcast devices can be accessed from C without changing the abi.
behold the mind
inspired by Dreamcast
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

what flags i have do use for the entry assembler file ? cant get a clean compile or link of any startup file ....

btw : how exactly is the syntax to link all the object files to a elf ? ive used the standart kos makefile but idont think this is right ...
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 5 times

Post by nymus »

If I may offer a better response than the one I gave before...

To get your kernel booting on Dreamcast, you do not really need a
bootloader; just a little knowledge of the cpu and the bios.

First, the sh4 has different address spaces for user and privileged
modes. In privileged mode, the address space is divided into 4 smaller
address spaces P0-P4. These address spaces affect which cpu resources
and external ram can be accessed. If you are not using the mmu (an
assumption I will make), then established convention is to divide your
work among P1, P2 and P4.

P1 is cacheable and is where your normal programs will run. P2 is
non-cacheable and is where your kernel will run and set critical cpu
devices (cache, mmu). P4 cannot run programs but is where cpu devices
have been mmapped. The bios loads binaries into the P1 area so the
first thing you'll see being done is to enter P2 area by masking the
address of a setup function where the cache is configured (mmu might
also be configured if you need it).

Since it's just your kernel you want to test, you don't have to worry
about what entries gcc needs (because you're coding in C) so you can
just interface to the main entry point of your program directly. I
usually just give my "main" a different name, like "void start()" and
call it directly afer setting up the cpu. You'll just need to learn a
little sh4 asm to save/restore cpu context etc.

To handle exceptions on the sh4 you have to set VBR manually as the
base and offset the entry points manually as well

You can get other io code eg printing text, serial, maple from
existing libs (libdream, kos, ronin, dcos, netbsd)

eg in startup.s

Code: Select all

	.global start
start:
	! First, make sure to run in the P2 area
	mova	setup_cache, r0
	mov.l	p2_mask, r1
	or	r1, r0
	jmp	@r0
	nop
setup_cache:
	! Now that we are in P2, it's safe
	! to enable the cache
	mov.l	ccr_addr, r0
	mov.w	ccr_data, r1
	mov.l	r1, @r0
	! After changing CCR, eight instructions
	! must be executed before it's safe to enter
	! a cached area such as P1
	mov.l	main_addr, r0	! 1
	mov	#0, r1		! 2
	nop			! 3
	nop			! 4
	nop			! 5
	nop			! 6
	nop			! 7
	nop			! 8
	jmp	@r0		! go
	mov	r1, r0

.balign 4
p2_mask: .long 0xa0000000
main_addr: .long main
ccr_addr: .long 0xff00001c
ccr_data: .word 0x090d

main:
	!! do basic config eg set up fpu, stack...

	!! set vbr

	mova   expt_entry, r0
	ldc    r0, vbr

	!! you have set up everything, so call your C start
	mov.l	start_addr, r0
	jmp	@r0
	nop
exit:
	!! restore previous stack etc and return 
	!! in this case return to dcload
	mov.l	dcload_syscall, r0
	mov.l	@r0, r0
	jmp	@r0
	mov	#15, r4

.balign 4
start_addr:	.long _start
dcload_syscall: .long 0x8c004008

expt_entry:	.space 0x100
expt:
	!! write code to get expevt/tra and call your
	!! exception-handling code accordingly
expt_end:	.space 0x400 - (. - expt_entry)
mmu_expt:
	!! handle mmu expt
mmu_expt_end:	.space 0x600 - (. - expt_entry)
intr_exp:
	!! handle interrupts

.balign 4
tra_addr:	. long 0xff000020
expevt_addr:	. long 0xff000024
intevt_addr:	. long 0xff000028
In your C program main.c

Code: Select all

#include <sys_header.h>
#include "my_header.h"

/* this will be assembled to _start accessed by startup.s above */

void start() {
     // do whatever
}

You can compile such a program by passing the following command line
to sh-elf-gcc

Code: Select all

sh-elf-gcc -m4 -ml -O2 -fomit-frame-pointer -Wl,-EL,-Ttext,0x8c010000,-e,start
-nostdlib -nostartfiles startup.s main.c $(MY_MODULES) -o kernel.elf -lgcc
This should work for the kernel. If you want your kernel to start user
programs properly, you'll have to mess with calling gcc constructors
etc

You don't have to worry too much about exceptions at first, especially
if you're using dcload which has exception handling code that restarts
it.
behold the mind
inspired by Dreamcast
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

yeah thank you very much ^^

but i get always an error :

cannot find -lgcc



something wrong with my compiler set ?
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

complete error message :

/usr/local/dc/sh-elf/lib/gcc/sh-elf/3.4.2/../../../sh-elf/bin/ld: cannot find -lgcc

collect2: ld returned 1 exit status


with out the -lgcc flag i get many , many errors ^^
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 5 times

Post by nymus »

You need -lgcc because it provides internal functions used by gcc. It is automatically installed and ld should know where to find it... I'm not sure, but I think your compiler set may have a problem. Have you used it to compile working kos programs? Check that libgcc.a exists in "sh-elf/lib/gcc-lib/[version]/m4/ml" (not too sure about last dirs
behold the mind
inspired by Dreamcast
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Post by BlueCrab »

usually its ml/m4-single-only for the last two dirs. It will Always be ml/[m4, m4-single, m4-single-only, m2, m3e (depending on flags passed)] for the dreamcast (ml = little endian).
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

yes it exists .... ive installted it from this DCDev iso ... and most of the kos apps run so it should be right installed ...
User avatar
Masen
DCEmu Freak
DCEmu Freak
Posts: 68
Joined: Mon Mar 28, 2005 1:35 pm
Has thanked: 0
Been thanked: 0

Post by Masen »

but the path is wrong is should be usr/local/dc/sh-elf/lib/gcc/sh-elf/3.4.2/../../../../sh-elf/bin/ld
instead of /usr/local/dc/sh-elf/lib/gcc/sh-elf/3.4.2/../../../sh-elf/bin/ld

so 4 times the ../ not 3 ... one ../ is missing ... how can i correct it ??? and why is it wrong ?
Post Reply