Skip to content

Commit 64bd12d

Browse files
committed
Clean up the memory arrangement.
Now there's 32K of RAM from the top of the SRAM region (24K from the striped block, plus SRAM_BLOCK4 and SRAM_BLOCK5) and from within that is the Core 0 stack, the Core 1 stack (a static mut array) and all the other global variables. Tested OK.
1 parent 8a19965 commit 64bd12d

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

memory.x

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,15 @@ MEMORY {
2525
/*
2626
* This is the bottom of the four striped banks of SRAM in the RP2040.
2727
*/
28-
RAM_OS : ORIGIN = 0x20000000, LENGTH = 0x39000
28+
RAM_OS : ORIGIN = 0x20000000, LENGTH = 232K
2929
/*
30-
* This is the top of the four striped banks of SRAM in the RP2040.
30+
* This is the top of the four striped banks of SRAM in the RP2040, plus SRAM_BANK4 and SRAM_BANK5.
3131
*
32-
* We give ourselves size 4K pages [0x39_000..0x3E_FFF]
32+
* We give ourselves eight 4K pages [0x3A_000..0x41_FFF]
3333
*/
34-
RAM : ORIGIN = 0x20039000, LENGTH = 24K
35-
/*
36-
* This 4K from the top of striped RAM, plus the fifth bank - another a 4KB
37-
* block. We use this for Core 0 Stack. We tried 4K but it wasn't enough.
38-
*/
39-
RAM_CORE0_STACK : ORIGIN = 0x2003F000, LENGTH = 8K
40-
/*
41-
* This is the sixth bank, a 4KB block. We use this for Core 1 Stack.
42-
* As of 0.5.1 Pico BIOS uses about 316 bytes of this but we give it the
43-
* full 4K so it can have uncontended access to this SRAM bank.
44-
*/
45-
RAM_CORE1_STACK : ORIGIN = 0x20041000, LENGTH = 4K
34+
RAM : ORIGIN = 0x2003A000, LENGTH = 32K
4635
}
4736

48-
/*
49-
* This is where the call stack for Core 0 will be located. The stack is of
50-
* the full descending type.
51-
*/
52-
_stack_start = ORIGIN(RAM_CORE0_STACK) + LENGTH(RAM_CORE0_STACK);
53-
_stack_bottom = ORIGIN(RAM_CORE0_STACK);
54-
_stack_len = LENGTH(RAM_CORE0_STACK);
55-
56-
/*
57-
* This is where the call stack for Core 1 will be located.
58-
*/
59-
_core1_stack_bottom = ORIGIN(RAM_CORE1_STACK);
60-
_core1_stack_len = LENGTH(RAM_CORE1_STACK);
61-
6237
/*
6338
* Export some symbols to tell the BIOS where it might find the OS.
6439
*/

src/main.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ pub static OS_IMAGE: [u8; include_bytes!("thumbv6m-none-eabi-flash1002-libneotro
249249
/// is currently active (low).
250250
static INTERRUPT_PENDING: AtomicBool = AtomicBool::new(false);
251251

252+
/// Stack for Core 1
253+
static mut CORE1_STACK: [usize; 256] = [0; 256];
254+
252255
/// The table of API calls we provide the OS
253256
static API_CALLS: common::Api = common::Api {
254257
api_version_get,
@@ -342,14 +345,17 @@ fn main() -> ! {
342345
{
343346
// Paint the stack
344347
extern "C" {
345-
static mut _stack_bottom: usize;
346-
static mut _stack_len: usize;
348+
static mut __sheap: usize;
349+
static mut _stack_start: usize;
347350
}
351+
let stack_len = unsafe {
352+
(&_stack_start as *const usize as usize) - (&__sheap as *const usize as usize)
353+
};
348354
// But not the top 64 words, because we're using the stack right now!
349355
let stack = unsafe {
350356
core::slice::from_raw_parts_mut(
351-
&mut _stack_bottom as *mut usize,
352-
(&mut _stack_len as *const _ as usize / core::mem::size_of::<usize>()) - 256,
357+
&mut __sheap as *mut usize,
358+
(stack_len / core::mem::size_of::<usize>()) - 256,
353359
)
354360
};
355361
info!("Painting {:?}", stack.as_ptr_range());
@@ -2120,17 +2126,21 @@ fn IO_IRQ_BANK0() {
21202126
#[cfg(feature = "check-stack")]
21212127
fn check_stacks() {
21222128
extern "C" {
2123-
static _stack_bottom: usize;
2124-
static _stack_len: usize;
2125-
static _core1_stack_bottom: usize;
2126-
static _core1_stack_len: usize;
2129+
static mut __sheap: usize;
2130+
static mut _stack_start: usize;
21272131
}
2128-
let p = unsafe { &_stack_bottom as *const usize };
2129-
let total_bytes = unsafe { &_stack_len as *const usize as usize };
2130-
check_stack(p, total_bytes, CORE0_STACK_PAINT_WORD);
2131-
let p = unsafe { &_core1_stack_bottom as *const usize };
2132-
let total_bytes = unsafe { &_core1_stack_len as *const usize as usize };
2133-
check_stack(p, total_bytes, CORE1_STACK_PAINT_WORD);
2132+
let stack_len =
2133+
unsafe { (&_stack_start as *const usize as usize) - (&__sheap as *const usize as usize) };
2134+
check_stack(
2135+
unsafe { &__sheap as *const usize },
2136+
stack_len,
2137+
CORE0_STACK_PAINT_WORD,
2138+
);
2139+
check_stack(
2140+
unsafe { CORE1_STACK.as_ptr() },
2141+
unsafe { CORE1_STACK.len() * core::mem::size_of::<usize>() },
2142+
CORE1_STACK_PAINT_WORD,
2143+
);
21342144
}
21352145

21362146
/// Dummy stack checker that does nothing

src/vga/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,33 +1027,30 @@ pub fn init(
10271027
// cannot be reconfigured at a later time, but they do keep on running
10281028
// as-is.
10291029

1030-
let core1_stack: &'static mut [usize] = unsafe {
1031-
extern "C" {
1032-
static mut _core1_stack_bottom: usize;
1033-
static mut _core1_stack_len: usize;
1030+
unsafe {
1031+
for b in super::CORE1_STACK.iter_mut() {
1032+
*b = super::CORE1_STACK_PAINT_WORD;
10341033
}
1035-
core::slice::from_raw_parts_mut(
1036-
&mut _core1_stack_bottom as *mut _,
1037-
&mut _core1_stack_len as *const _ as usize / core::mem::size_of::<usize>(),
1038-
)
1039-
};
1040-
1041-
for b in core1_stack.iter_mut() {
1042-
*b = super::CORE1_STACK_PAINT_WORD;
10431034
}
10441035

10451036
debug!(
10461037
"Core 1 stack: {:08x}, {} bytes",
1047-
core1_stack.as_ptr(),
1048-
core1_stack.len()
1038+
unsafe { super::CORE1_STACK.as_ptr() },
1039+
unsafe { super::CORE1_STACK.len() * core::mem::size_of::<usize>() }
10491040
);
10501041

10511042
// No-one else is looking at this right now.
10521043
unsafe {
10531044
TEXT_COLOUR_LOOKUP.init(&VIDEO_PALETTE);
10541045
}
10551046

1056-
multicore_launch_core1_with_stack(core1_main, core1_stack, ppb, fifo, psm);
1047+
multicore_launch_core1_with_stack(
1048+
core1_main,
1049+
unsafe { &mut super::CORE1_STACK },
1050+
ppb,
1051+
fifo,
1052+
psm,
1053+
);
10571054

10581055
debug!("Core 1 running");
10591056
}

0 commit comments

Comments
 (0)