From f529a3b0f8ee24229997e995021a2ee8cb25f065 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <clyne@bitgloo.com>
Date: Sat, 5 Oct 2024 12:13:49 -0400
Subject: initial commit

---
 .gitignore      |  3 +++
 build.zig       | 23 +++++++++++++++++++++++
 link.ld         | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/bootstrap.s | 38 ++++++++++++++++++++++++++++++++++++++
 src/main.zig    | 13 +++++++++++++
 5 files changed, 121 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 build.zig
 create mode 100644 link.ld
 create mode 100644 src/bootstrap.s
 create mode 100644 src/main.zig

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6e988fa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.sw*
+.zig-cache/
+zig-out/
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..10fe592
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,23 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const target = b.resolveTargetQuery(.{
+        .cpu_arch = .thumb,
+        .os_tag = .freestanding,
+        .abi = .none
+    });
+    const optimize = .Debug; // .ReleaseSmall
+
+    const exe = b.addExecutable(.{
+        .name = "stm32",
+        .root_source_file = b.path("src/main.zig"),
+        .target = target,
+        .optimize = optimize,
+        .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
new file mode 100644
index 0000000..403ba71
--- /dev/null
+++ b/link.ld
@@ -0,0 +1,44 @@
+ENTRY(_start)
+
+/* description of memory regions */
+MEMORY {
+	FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
+	RAM (rwx)  : ORIGIN = 0x20000000,  LENGTH = 96K
+}
+
+/* description of ELF sections */
+SECTIONS {
+	.text : {
+		. = ALIGN(8);
+		*(.isr_vector)
+		*(.text)
+		*(.text*)
+	} > FLASH
+
+	/* read-only data sections */
+	.rodata : {
+		. = ALIGN(8);
+		*(.rodata)
+		*(.rodata*)
+	} > FLASH
+
+	/* initialized data */
+	_sidata = LOADADDR(.data);
+	.data : {
+		. = ALIGN(8);
+		_sdata = .;
+		*(.data)
+		. = ALIGN(8);
+		_edata = .;
+	} > RAM AT > FLASH
+
+	/* uninitialized data */
+	.bss : {
+		. = ALIGN(8);
+		_sbss = .;
+		*(.bss)
+		. = ALIGN(8);
+		_ebss = .;
+	} > RAM
+}
+
diff --git a/src/bootstrap.s b/src/bootstrap.s
new file mode 100644
index 0000000..22f66f0
--- /dev/null
+++ b/src/bootstrap.s
@@ -0,0 +1,38 @@
+.cpu cortex-m4
+.thumb
+
+.extern _start
+.extern fault_handler
+
+.section .text
+
+fault1:
+    mov r0, 1
+    b fault_handler
+fault2:
+    mov r0, 2
+    b fault_handler
+fault3:
+    mov r0, 3
+    b fault_handler
+fault4:
+    mov r0, 4
+    b fault_handler
+fault5:
+    mov r0, 5
+    b fault_handler
+
+.section .bss
+.skip 128
+stack_top:
+
+.section .isr_vector
+    .word stack_top
+    .word _start
+    .word fault1
+    .word fault2
+    .word fault3
+    .word fault4
+    .word fault5
+    .skip 4 * 91
+
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..de602dd
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,13 @@
+//! By convention, main.zig is where your main function lives in the case that
+//! you are building an executable. If you are making a library, the convention
+//! is to delete this file and start with root.zig instead.
+//const std = @import("std");
+
+export fn _start() callconv(.C) noreturn {
+    while (true) {}
+}
+
+export fn fault_handler() callconv(.C) void {
+    while (true) {}
+}
+
-- 
cgit v1.2.3