aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-10-06 16:01:53 -0400
committerClyne Sullivan <clyne@bitgloo.com>2024-10-06 16:01:53 -0400
commitf4bf5658970543f26f25816b9123ecfd258bdd42 (patch)
tree80eda13e97de4bb0f7a8dc4c6b135b66aa1f1147
parentb262bb6b956bc044144d47a5e037d4af98a19a30 (diff)
better startup: all zig, bss and data init'd
-rw-r--r--build.zig3
-rw-r--r--link.ld10
-rw-r--r--src/bootstrap.s13
-rw-r--r--src/main.zig2
-rw-r--r--src/startup.zig41
5 files changed, 48 insertions, 21 deletions
diff --git a/build.zig b/build.zig
index 00d19c4..8493bbb 100644
--- a/build.zig
+++ b/build.zig
@@ -10,14 +10,13 @@ pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "stm32",
- .root_source_file = b.path("src/main.zig"),
+ .root_source_file = b.path("src/startup.zig"),
.target = target,
//.optimize = .Debug,
.optimize = .ReleaseSafe,
.linkage = .static,
});
- exe.addAssemblyFile(b.path("src/bootstrap.s"));
exe.setLinkerScriptPath(b.path("link.ld"));
b.installArtifact(exe);
diff --git a/link.ld b/link.ld
index e777ac4..f5011ba 100644
--- a/link.ld
+++ b/link.ld
@@ -23,24 +23,24 @@ SECTIONS {
} > FLASH
/* initialized data */
- _sidata = LOADADDR(.data);
+ __data_flash = LOADADDR(.data);
.data : {
. = ALIGN(8);
- _sdata = .;
+ __data = .;
*(.data)
*(.data*)
. = ALIGN(8);
- _edata = .;
+ __data_size = . - __data;
} > RAM AT > FLASH
/* uninitialized data */
.bss : {
. = ALIGN(8);
- _sbss = .;
+ __bss = .;
*(.bss)
*(.bss*)
. = ALIGN(8);
- _ebss = .;
+ __bss_size = . - __bss;
} > RAM
.stack : {
diff --git a/src/bootstrap.s b/src/bootstrap.s
deleted file mode 100644
index 612fac3..0000000
--- a/src/bootstrap.s
+++ /dev/null
@@ -1,13 +0,0 @@
-.cpu cortex-m4
-.thumb
-
-.extern _start
-.extern _tstack
-
-.section .vector_table
-.global init_vector_table
-init_vector_table:
- .word _tstack
- .word _start
- .skip 4 * 14
-
diff --git a/src/main.zig b/src/main.zig
index d6a1aa4..99cb451 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -8,7 +8,7 @@ const gpioa = gpio.gpioa;
const gpioc = gpio.gpioc;
const tick = timer.systick;
-export fn _start() callconv(.C) noreturn {
+pub fn main() void {
cpu.interrupt_disable();
interrupt.initialize();
interrupt.register(.SVCall, svcall);
diff --git a/src/startup.zig b/src/startup.zig
new file mode 100644
index 0000000..f7c7712
--- /dev/null
+++ b/src/startup.zig
@@ -0,0 +1,41 @@
+const main = @import("main.zig");
+
+extern const __data_flash: u32;
+extern const __data: u32;
+extern const __data_size: u32;
+extern const __bss: u32;
+extern const __bss_size: u32;
+
+comptime {
+ asm (
+ \\.extern _start
+ \\.extern _tstack
+ \\
+ \\.section .vector_table
+ \\.global init_vector_table
+ \\init_vector_table:
+ \\ .word _tstack
+ \\ .word _start
+ \\ .skip 4 * 14
+ );
+}
+
+inline fn zero_bss() void {
+ const bss: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__bss));
+ @memset(bss[0..@intFromPtr(&__bss_size)], 0);
+}
+
+inline fn copy_data() void {
+ const data_flash: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__data_flash));
+ const data: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__data));
+ const data_size = @intFromPtr(&__data_size);
+ @memcpy(data[0..data_size], data_flash[0..data_size]);
+}
+
+export fn _start() callconv(.C) noreturn {
+ zero_bss();
+ copy_data();
+ main.main();
+ while (true) {}
+}
+