Tinkering with Dreamcast's Sh4's architecture

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.
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Dec 24, 2011 9:10 am

I've been researching more and more about Processors and x86 Assembly.

I've had a hard time finding decent resources and general information on the SH4 (or SuperH) CPU however from what I can find it's optimized for vector and matrix calculations. Actually reserving instructions and registers. I believe AMD's 3DNow takes a similar approach.

So I'm wondering, to what extent to the KOS utilize this functionality?
And is this what's actually going on? Like registers and instructions being reserved or am I just completely deluded? :lol:

Also, if anyone knows a decent Sh4 assembler, or any good resources on this CPU then I'd very much appreciate it! :grin:
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 351
Joined: Wed Mar 30, 2011 12:13 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by RyoDC » Sat Dec 24, 2011 10:03 am

1.Marcus Site. http://mc.pp.se/dc/cpu.html
Look at this page you found there everything that you need (programming and hardware manual covers almost everything. after reading it you can even develop your own board on which you can set sh4 processor).

2. About registers and etc. KOS using libkallisti.a as a main source of raw machine code. So you can look through this file, or even push it to the disassembler\debugger, and run for the step tracing, to see, what is actually happens with registers, memory, and etc. The one problem that I didn't find one on the net (I mean debugger). One had nullDC but I didn't found it good. there's still gdb debugger, and IDA, but IDa sucks with dreamcast code, so...
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Dec 24, 2011 10:08 am

RyoDC wrote:Marcus Site. http://mc.pp.se/dc/cpu.html
Look at this page you found there everything that you need (programming and hardware manual covers almost everything. after reading it you can even develop your own board on which you can set sh4 processor).
Thanks much! That one link is like an encyclopedia! :o
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 351
Joined: Wed Mar 30, 2011 12:13 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by RyoDC » Sat Dec 24, 2011 10:13 am

Np man. Always ready to help.
How do I try to build a Dreamcast toolchain:
Image
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Dec 24, 2011 11:14 am

I'm still stuck on an Assembler though, Hitachi's site seems long gone. I'm guessing you'd have to write your own -gulp- :(

So if anyone has any info on that, much appreciated again! :grin:

EDIT:
I just had a thought, the KOS development kit comes with GCC ready to compile straight out the box. Does this mean I've had an assembler all this time?
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 351
Joined: Wed Mar 30, 2011 12:13 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by RyoDC » Sat Dec 24, 2011 11:24 am

So you need an assembler?
Well, Hitachi site is not gone, but Hitachi, as a manufacturer of semiconductor devices, yes, it have. Now it's Renesas company. Anyway. Aboutt GCC.
Use this link.
http://www.delorie.com/djgpp/v2faq/faq8_20.html
How do I try to build a Dreamcast toolchain:
Image
Ayla
Insane DCEmu
Insane DCEmu
Posts: 131
Joined: Thu Apr 03, 2008 7:01 am

Re: Tinkering with Dreamcast's Sh4's architecture

Post by Ayla » Sat Dec 24, 2011 11:40 am

GCC (actually Binutils) provides an assembler. Just compile your .s file with kos-cc.
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am

Re: Tinkering with Dreamcast's Sh4's architecture

Post by Chilly Willy » Sat Dec 24, 2011 11:48 am

Ayla wrote:GCC (actually Binutils) provides an assembler. Just compile your .s file with kos-cc.
For those not familiar with gcc and assembly: A ".s" extension means the file gets passed directly to the assembler and must contain nothing but assembler friendly syntax. A ".S" extension (note that the s is capitalized) means the file is passed through the C preprocessor first before being sent to the assembler, so the file can have C friendly syntax for things like enum and define for stuff you'll use. So a .s file would have ".equ some_symbol, 1" while a .S file can have "#define some_symbol 1".
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Dec 24, 2011 12:04 pm

Well awesome!
You've all been a *huge* help. I'll see what I can come up with! :grin:
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Wed Jan 04, 2012 4:22 pm

Okay, after looking further into the links provided (thanks again!), The Sh4 CPU does indeed have opcodes/registers for optimizing vector calcualtions. Which is awesome, but before I start screwing around with this, I've got a few questions about the KOS's PVR API. Sorry if any of these are obvious/idiotic.

The actual PVR headers are located under kernel/arch/pvr?
Also, is there any assembly involved with the PVR API? If so, where are these files in the KOS repo?
And, one other question... Does the PVR API take advantage of this?
I know I already asked this question, but I probably shouldn't be attempting to implement functionality that already exists.

Thanks!
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am

Re: Tinkering with Dreamcast's Sh4's architecture

Post by Chilly Willy » Wed Jan 04, 2012 4:49 pm

Just doing

Code: Select all

#include <kos.h>
At the start of the file will take care of any pvr includes needed. Look at the examples in

kos/examples/dreamcast/pvr
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5274
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: Tinkering with Dreamcast's Sh4's architecture

Post by BlueCrab » Wed Jan 04, 2012 4:55 pm

The main include file for the PVR is kos/kernel/arch/dreamcast/include/dc/pvr.h (documentation). Note that this particular interface assumes that any 3D-related transformations you want done on the primitives are already done before feeding them in. It simply takes the data and feeds it directly to the hardware.

You may also be interested in kos/kernel/arch/dreamcast/include/dc/matrix.h (documentation) and kos/kernel/arch/dreamcast/include/dc/matrix3d.h (documentation), as they are the headers related to any matrix-type stuff that is in KOS proper. The 2ndmix example shows how to use some of that stuff, IIRC.
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 351
Joined: Wed Mar 30, 2011 12:13 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by RyoDC » Fri Jan 06, 2012 3:22 pm

Crab, one question. As far as i concerned, PowerVR chip doesn't support hardware lightning, I mean (besides of shading modes), light sources. So, that mean, that I can use my custom lightning algorithms for different types of surfaces, isn't it?
How do I try to build a Dreamcast toolchain:
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5274
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: Tinkering with Dreamcast's Sh4's architecture

Post by BlueCrab » Fri Jan 06, 2012 5:06 pm

There is no hardware on the PVR to do lighting. You're responsible for calculating any lighting effects yourself and modifying what's sent to the PVR appropriately (mostly by changing the vertex colors to account for the light).
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 351
Joined: Wed Mar 30, 2011 12:13 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by RyoDC » Sat Jan 07, 2012 10:04 am

Crab, how do I use the bump mapping?
How do I try to build a Dreamcast toolchain:
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5274
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: Tinkering with Dreamcast's Sh4's architecture

Post by BlueCrab » Sat Jan 07, 2012 11:01 am

Search is your friend. Here's a post with links to an example of how to do it: viewtopic.php?p=816595#p816595
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Jan 21, 2012 2:27 pm

Sorry for reviving such an old thread, I just really didn't want to make another considering it's a question related to the adventure started in this thread :lol:

I know the KOS has this stuff implemented already, I think it's in "kernel/arch/dreamcast/include/dc/math.h"?

Either way, for learning purposes I've been trying to utilize the SH4's vector functionality.

I took a look a the KOS's fmath.h header, which seems to inline assembly optimizations for shitton of stuff. For example, faster square roots, vector products, matrix multiplication etc.

Anywhoo, enough blabbering lol.
The KOS has this macro here:

Code: Select all

#define __fipr(x, y, z, w, a, b, c, d) ({ \
	register float __x __asm__("fr0") = (x); \
	register float __y __asm__("fr1") = (y); \
	register float __z __asm__("fr2") = (z); \
	register float __w __asm__("fr3") = (w); \
	register float __a __asm__("fr4") = (a); \
	register float __b __asm__("fr5") = (b); \
	register float __c __asm__("fr6") = (c); \
	register float __d __asm__("fr7") = (d); \
	__asm__ __volatile__( \
		"fipr	fv4,fv0" \
		: "+f" (__w) \
		: "f" (__x), "f" (__y), "f" (__z), "f" (__w), \
		  "f" (__a), "f" (__b), "f" (__c), "f" (__d) \
		); \
		__w; })
Which clearly calculates the inner product of a 4D vector. But I'm very confused as to exactly how this works.

The constraints passed to GCC just say throw it into a floating point register, but they would need to be in a ordered fashion for the results to be accurate. Which leads me to believe that the values are being placed into the FR registers when they're declared like this:

Code: Select all

register float __x __asm__("fr0") = (x); \
But looking into the "register" keyword, GCC can never promise they will actually be stored in a register.

Sorry if I've been too vague, I'm just quite confused. This is more of a question about GCC, but this seems quite different to any x86 inlined assembly I've seen.

EDIT: Just correct a few things...

Thanks!
Ayla
Insane DCEmu
Insane DCEmu
Posts: 131
Joined: Thu Apr 03, 2008 7:01 am

Re: Tinkering with Dreamcast's Sh4's architecture

Post by Ayla » Sat Jan 21, 2012 2:53 pm

The "register" keyword tells GCC that the associated variable should always be kept in a register and not on memory.

The following code simply associate the __x float variable with the fr0 register.

Code: Select all

register float __x __asm__("fr0")
Now each time you do __x = 4 on your C/C++ code, the value 4 will be stored on the fr0 register, and not on memory or on any other register.
Ex-Cyber
DCEmu User with No Life
DCEmu User with No Life
Posts: 3641
Joined: Sat Feb 16, 2002 1:55 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by Ex-Cyber » Sat Jan 21, 2012 3:05 pm

I think the way it works is that GCC guarantees the mapping when those variables are referenced by name as operands in an asm block. However, the mapping is not necessarily maintained beyond that (e.g. the variables might "really" live in memory and are just copied in/out of the specified registers by code generated around the asm block). AFAIK, the "register" keyword is not actually guaranteed to do anything in ANSI C generally, but sadly my copy of K&R is in my cube at the moment.

edit: it might be guaranteed to generate an error if the & operator is used on a register variable, but I don't know whether that's required or the compiler can implicitly convert it to a memory variable.
Last edited by Ex-Cyber on Sat Jan 21, 2012 3:20 pm, edited 1 time in total.
"You know, I have a great, wonderful, really original method of teaching antitrust law, and it kept 80 percent of the students awake. They learned things. It was fabulous." -- Justice Stephen Breyer
N64VSNES
DCEmu Freak
DCEmu Freak
Posts: 65
Joined: Sun Apr 10, 2011 12:05 pm

Re: Tinkering with Dreamcast's Sh4's architecture

Post by N64VSNES » Sat Jan 21, 2012 3:16 pm

Ah, seems to make sense.

So really, even though it's not in that register, any inlined code can treat it as if it really is?

But then, why does the inline constraints have to directly specify the input operands? :?
Post Reply