Skip to content

Commit 09aea2b

Browse files
committed
Support Dreamcasts with 32MB system RAM
Only the 9.3.0 patch is updated!
1 parent 6830519 commit 09aea2b

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

kernel/arch/dreamcast/include/arch/arch.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ void __crtend_pullin();
136136
*/
137137
int mm_init();
138138

139+
/** \brief Determine how much addressable memory is present. On Dreamcast
140+
this checks the MCR to determine which addressing is being used.
141+
Only supports pristine 16MB SDRAM and 32MB SDRAM as described at
142+
https://tsowell.github.io/2020/06/21/dreamcast-32mb-ram-upgrade.html
143+
\return The address that forms the upper bound of system RAM.
144+
*/
145+
uint32 mm_top();
146+
139147
/** \brief Request more core memory from the system.
140148
\param increment The number of bytes requested.
141149
\return A pointer to the memory.
@@ -354,17 +362,13 @@ const char *kos_get_authors(void);
354362
*/
355363
#define arch_fptr_next(fptr) (*((uint32*)(fptr+4)))
356364

357-
#ifndef _arch_sub_naomi
358365
/** \brief Returns true if the passed address is likely to be valid. Doesn't
359366
have to be exact, just a sort of general idea.
360367
361368
\return Whether the address is valid or not for normal
362369
memory access.
363370
*/
364-
#define arch_valid_address(ptr) ((ptr_t)(ptr) >= 0x8c010000 && (ptr_t)(ptr) < 0x8d000000)
365-
#else
366-
#define arch_valid_address(ptr) ((ptr_t)(ptr) >= 0x8c010000 && (ptr_t)(ptr) < 0x8e000000)
367-
#endif
371+
#define arch_valid_address(ptr) ((ptr_t)(ptr) >= 0x8c010000 && (ptr_t)(ptr) < mm_top())
368372

369373
__END_DECLS
370374

kernel/arch/dreamcast/kernel/mm.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <arch/irq.h>
2020
#include <stdio.h>
2121

22+
#define MCR (*((volatile uint32_t *) 0xff800014))
23+
2224
/* The end of the program is always marked by the '_end' symbol. So we'll
2325
longword-align that and add a little for safety. sbrk() calls will
2426
move up from there. */
@@ -34,6 +36,21 @@ int mm_init() {
3436
return 0;
3537
}
3638

39+
/* Return address at top of system RAM */
40+
uint32 mm_top() {
41+
if (((MCR >> 3) & 0x07) == 3) {
42+
/* AMX 3, assuming 32MB SDRAM */
43+
return 0x8e000000;
44+
}
45+
else {
46+
#ifndef _arch_sub_naomi
47+
return 0x8d000000;
48+
#else
49+
return 0x8e000000;
50+
#endif
51+
}
52+
}
53+
3754
/* Simple sbrk function */
3855
void* mm_sbrk(unsigned long increment) {
3956
int old;
@@ -46,7 +63,7 @@ void* mm_sbrk(unsigned long increment) {
4663

4764
sbrk_base = (void *)(increment + (unsigned long)sbrk_base);
4865

49-
if(((uint32)sbrk_base) >= (0x8d000000 - 65536)) {
66+
if(((uint32)sbrk_base) >= (mm_top() - 65536)) {
5067
dbglog(DBG_DEAD, "Requested sbrk_base %p, was %p, diff %lu\n",
5168
sbrk_base, base, increment);
5269
arch_panic("out of memory; about to run over kernel stack");

kernel/arch/dreamcast/kernel/stack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void arch_stk_trace_at(uint32 fp, int n) {
2929
dbgio_printf("-------- Stack Trace (innermost first) ---------\n");
3030

3131
while(fp != 0xffffffff) {
32-
if((fp & 3) || (fp < 0x8c000000) || (fp > 0x8d000000)) {
32+
if((fp & 3) || (fp < 0x8c000000) || (fp > mm_top())) {
3333
dbgio_printf(" (invalid frame pointer)\n");
3434
break;
3535
}

kernel/arch/dreamcast/kernel/startup.s

+14-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ init:
7171
! Save the current stack, and set a new stack (higher up in RAM)
7272
mov.l old_stack_addr,r0
7373
mov.l r15,@r0
74-
mov.l new_stack,r15
74+
! Set stack based on MCR
75+
mov.l new_stack_16m,r15
76+
mov.l mcr,r1
77+
mov.l @r1,r0
78+
and #0x38,r0
79+
cmp/eq #0x18,r0
80+
bf mcr_16m
81+
mov.l new_stack_32m,r15
82+
mcr_16m:
7583

7684
! Save VBR
7785
mov.l old_vbr_addr,r0
@@ -173,8 +181,12 @@ old_stack_addr:
173181
__arch_old_stack:
174182
old_stack:
175183
.long 0
176-
new_stack:
184+
new_stack_16m:
177185
.long 0x8d000000
186+
new_stack_32m:
187+
.long 0x8e000000
188+
mcr:
189+
.long 0xff800014
178190
p2_mask:
179191
.long 0xa0000000
180192
setup_cache_addr:

kernel/mm/malloc_debug.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void *malloc(size_t amt) {
108108
spinlock_unlock(&mutex);
109109

110110
printf("Thread %d/%08lx allocated %d bytes at %08lx; %08lx left\n",
111-
ctl->thread, ctl->addr, ctl->size, space, 0x8d000000 - (uint32)sbrk(0));
111+
ctl->thread, ctl->addr, ctl->size, space, mm_top() - (uint32)sbrk(0));
112112

113113
assert(!(((uint32)space) & 7));
114114

@@ -178,7 +178,7 @@ void free(void *block) {
178178
printf("Thread %d/%08x freeing block @ %08x\n",
179179
thd_current->tid, pr, (uint32)block);
180180

181-
if(((uint32)block) & 7 || (uint32)block < 0x8c010000 || (uint32)block >= 0x8d000000) {
181+
if(((uint32)block) & 7 || (uint32)block < 0x8c010000 || (uint32)block >= mm_top()) {
182182
printf(" ATTEMPT TO FREE INVALID ADDRESS!\n");
183183
spinlock_unlock(&mutex);
184184
return;

utils/dc-chain/patches/gcc-9.3.0-kos.diff

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ diff -ruN gcc-9.3.0/gcc/configure gcc-9.3.0-kos/gcc/configure
1313
diff -ruN gcc-9.3.0/libgcc/config/sh/crt1.S gcc-9.3.0-kos/libgcc/config/sh/crt1.S
1414
--- gcc-9.3.0/libgcc/config/sh/crt1.S 2020-03-12 07:07:23.000000000 -0400
1515
+++ gcc-9.3.0-kos/libgcc/config/sh/crt1.S 2020-04-03 16:07:04.540000000 -0400
16-
@@ -1,724 +1,197 @@
16+
@@ -1,724 +1,209 @@
1717
-/* Copyright (C) 2000-2019 Free Software Foundation, Inc.
1818
- This file was pretty much copied from newlib.
1919
+! KallistiOS ##version##
@@ -232,7 +232,15 @@ diff -ruN gcc-9.3.0/libgcc/config/sh/crt1.S gcc-9.3.0-kos/libgcc/config/sh/crt1.
232232
+ ! Save the current stack, and set a new stack (higher up in RAM)
233233
+ mov.l old_stack_addr,r0
234234
+ mov.l r15,@r0
235-
+ mov.l new_stack,r15
235+
+ ! Set stack based on MCR
236+
+ mov.l new_stack_16m,r15
237+
+ mov.l mcr,r1
238+
+ mov.l @r1,r0
239+
+ and #0x38,r0
240+
+ cmp/eq #0x18,r0
241+
+ bf mcr_16m
242+
+ mov.l new_stack_32m,r15
243+
+mcr_16m:
236244
+
237245
+ ! Save VBR
238246
+ mov.l old_vbr_addr,r0
@@ -896,8 +904,12 @@ diff -ruN gcc-9.3.0/libgcc/config/sh/crt1.S gcc-9.3.0-kos/libgcc/config/sh/crt1.
896904
+__arch_old_stack:
897905
+old_stack:
898906
+ .long 0
899-
+new_stack:
900-
+ .long 0x8d000000
907+
+new_stack_16m:
908+
+ .long 0x8d000000
909+
+new_stack_32m:
910+
+ .long 0x8e000000
911+
+mcr:
912+
+ .long 0xff800014
901913
+p2_mask:
902914
+ .long 0xa0000000
903915
+setup_cache_addr:

0 commit comments

Comments
 (0)