|
| 1 | +// Copyright (c) 2026 ETH Zurich and University of Bologna. |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Authors: |
| 6 | +// - Philippe Sauter <phsauter@iis.ee.ethz.ch> |
| 7 | + |
| 8 | +#include "uart.h" |
| 9 | +#include "print.h" |
| 10 | +#include "util.h" |
| 11 | +#include "config.h" |
| 12 | +#include "soc_ctrl.h" |
| 13 | + |
| 14 | +// maximum characters to read from user ROM string |
| 15 | +#define ROM_STRING_MAX_LEN 256 |
| 16 | + |
| 17 | + |
| 18 | +// set by croc_exception_handler on OBI load access fault (mcause == 5) |
| 19 | +volatile int obi_error = 0; |
| 20 | + |
| 21 | +// Overrides the default exception handler in crt0.S. |
| 22 | +// On an OBI load error: sets obi_error and advances mepc past the faulting load. |
| 23 | +void croc_exception_handler(uint32_t mcause) { |
| 24 | + if (mcause == 5) { // Load access fault |
| 25 | + obi_error = 1; |
| 26 | + uint32_t mepc; |
| 27 | + asm volatile("csrr %0, mepc" : "=r"(mepc)); |
| 28 | + asm volatile("csrw mepc, %0" :: "r"(mepc + 4)); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Attempt a 32-bit word read from addr. |
| 33 | +static int probe_read(uint32_t addr, uint32_t *val) { |
| 34 | + obi_error = 0; |
| 35 | + *val = *(volatile uint32_t *)addr; |
| 36 | + return obi_error; |
| 37 | +} |
| 38 | + |
| 39 | +// Print a null-terminated string from a memory-mapped ROM. |
| 40 | +// Reads one word at a time; stops on null terminator, OBI error, or max length. |
| 41 | +static void print_rom_str(uint32_t addr) { |
| 42 | + uint32_t word; |
| 43 | + for (uint32_t n = 0, off = 0; n < ROM_STRING_MAX_LEN; off += 4) { |
| 44 | + if (probe_read(addr + off, &word)) |
| 45 | + return; |
| 46 | + for (int b = 0; b < 4 && n < ROM_STRING_MAX_LEN; b++, n++) { |
| 47 | + char c = (char)((word >> (b * 8)) & 0xFF); |
| 48 | + if (!c) return; |
| 49 | + putchar(c); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Peripheral table: name (8 chars, space-padded for alignment) and base address |
| 55 | +static const struct { |
| 56 | + const char *name; |
| 57 | + uint32_t addr; |
| 58 | +} peripherals[] = { |
| 59 | + { "Debug ", DEBUG_BASE_ADDR }, |
| 60 | + { "Bootrom ", BOOTROM_BASE_ADDR }, |
| 61 | + { "CLINT ", CLINT_BASE_ADDR }, |
| 62 | + { "SoC Ctrl", SOCCTRL_BASE_ADDR }, |
| 63 | + { "UART ", UART_BASE_ADDR }, |
| 64 | + { "GPIO ", GPIO_BASE_ADDR }, |
| 65 | + { "Timer ", OBI_TIMER_BASE_ADDR }, |
| 66 | + { "iDMA ", IDMA_BASE_ADDR }, |
| 67 | + { "User ROM", USER_ROM_BASE_ADDR }, |
| 68 | +}; |
| 69 | +#define NUM_PERIPHERALS (sizeof(peripherals) / sizeof(peripherals[0])) |
| 70 | + |
| 71 | +int main() { |
| 72 | + uart_init(); |
| 73 | + |
| 74 | + // Read compile-time configuration from the SoC control info register |
| 75 | + uint32_t info = *reg32(SOCCTRL_BASE_ADDR, SOC_CTRL_INFO_REG_OFFSET); |
| 76 | + uint32_t version = ((info >> SOC_CTRL_INFO_VERSION_OFFSET) & SOC_CTRL_INFO_VERSION_MASK) + 1; |
| 77 | + uint32_t core_id = (info >> SOC_CTRL_INFO_CORE_ID_OFFSET) & SOC_CTRL_INFO_CORE_ID_MASK; |
| 78 | + uint32_t sram_banks = (info >> SOC_CTRL_INFO_SRAM_BANKS_OFFSET) & SOC_CTRL_INFO_SRAM_BANKS_MASK; |
| 79 | + uint32_t sram_words = ((info >> SOC_CTRL_INFO_SRAM_WORDS_OFFSET) & SOC_CTRL_INFO_SRAM_WORDS_MASK) * 64; |
| 80 | + uint8_t has_idma = (info >> SOC_CTRL_INFO_IDMA_BIT) & 1; |
| 81 | + uint8_t has_pmp = (info >> SOC_CTRL_INFO_PMP_BIT) & 1; |
| 82 | + |
| 83 | + // Read ISA extensions from the misa CSR (bits 0-25 map to extensions A-Z) |
| 84 | + uint32_t misa; |
| 85 | + asm volatile("csrr %0, misa" : "=r"(misa)); |
| 86 | + |
| 87 | + printf("Hello World from Croc v%x!\n", version); |
| 88 | + |
| 89 | + // SoC features |
| 90 | + printf(" iDMAEnable: %x\n", has_idma); |
| 91 | + |
| 92 | + // Core: type, ISA string from misa, optional PMP |
| 93 | + printf(" Core: "); |
| 94 | + printf(core_id == SOC_CTRL_INFO_CORE_ID_CVE2 ? "CVE2" : |
| 95 | + core_id == SOC_CTRL_INFO_CORE_ID_CUSTOM ? "custom" : "unknown"); |
| 96 | + printf(", RV32"); |
| 97 | + for (int i = 0; i < 26; i++) { |
| 98 | + if (misa & (1u << i)) |
| 99 | + putchar('A' + i); |
| 100 | + } |
| 101 | + putchar('\n'); |
| 102 | + printf(" PMPEnable: %x\n", has_pmp); |
| 103 | + |
| 104 | + // Memory |
| 105 | + printf(" SRAM: %xh banks x %xh words\n", sram_banks, sram_words); |
| 106 | + |
| 107 | + uart_write_flush(); |
| 108 | + |
| 109 | + // Scan all peripherals |
| 110 | + printf("Peripherals:\n"); |
| 111 | + uint32_t word; |
| 112 | + for (uint32_t i = 0; i < NUM_PERIPHERALS; i++) { |
| 113 | + printf(" "); |
| 114 | + printf(peripherals[i].name); |
| 115 | + printf(": "); |
| 116 | + if (probe_read(peripherals[i].addr, &word)) { |
| 117 | + printf("not present\n"); |
| 118 | + } else if (peripherals[i].addr == USER_ROM_BASE_ADDR) { |
| 119 | + // User ROM: print its null-terminated product string |
| 120 | + putchar('"'); |
| 121 | + print_rom_str(USER_ROM_BASE_ADDR); |
| 122 | + printf("\"\n"); |
| 123 | + } else { |
| 124 | + printf("present\n"); |
| 125 | + } |
| 126 | + } |
| 127 | + uart_write_flush(); |
| 128 | + |
| 129 | + return 0; |
| 130 | +} |
0 commit comments