1// Copyright © 2020-2021 Sören Tempel2//3// This program is free software: you can redistribute it and/or modify4// it under the terms of the GNU Affero General Public License as5// published by the Free Software Foundation, either version 3 of the6// License, or (at your option) any later version.7//8// This program is distributed in the hope that it will be useful, but9// WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11// Affero General Public License for more details.12//13// You should have received a copy of the GNU Affero General Public License14// along with this program. If not, see <https://www.gnu.org/licenses/>.1516const console = @import("console.zig");17const periph = @import("periph.zig");18const main = @import("main.zig");19const StackTrace = @import("std").builtin.StackTrace;2021// Bitmasks for modifying mcause CSR22const MCAUSE_EXPCODE = 0x0fff;23const MCAUSE_INT = 0x80000000;2425// Exception codes26const EXP_BREAKPOINT = 3;2728pub fn panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn {29 _ = error_return_trace; // unused3031 // Copied from the default_panic implementation32 @setCold(true);3334 // Write panic message, unbuffered to standard out35 console.print("PANIC: {s}\n", .{msg});3637 @breakpoint();38 while (true) {}39}4041export fn level1IRQHandler() void {42 const mcause = asm ("csrr %[ret], mcause"43 : [ret] "=r" (-> u32)44 );4546 const expcode: u32 = mcause & MCAUSE_EXPCODE;47 if ((mcause & MCAUSE_INT) == MCAUSE_INT) {48 periph.plic0.invokeHandler();49 } else {50 if (expcode == EXP_BREAKPOINT) {51 while (true) {}52 }53 @panic("unexpected trap");54 }55}5657export fn init() void {58 periph.init();59 console.print("Booting zig-riscv-embedded...\n", .{});6061 main.main() catch |err| {62 @panic(@errorName(err));63 };64}