Refactored exception routine, working trapa handler.

Signed-off-by: imi415 <imi415.public@gmail.com>
This commit is contained in:
imi415 2022-09-07 22:12:54 +08:00
parent 782308d696
commit d8e7b24931
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
5 changed files with 83 additions and 21 deletions

12
.clang-format Normal file
View File

@ -0,0 +1,12 @@
BasedOnStyle: Google
IndentWidth: 4
AlignConsecutiveMacros: AcrossEmptyLines
AlignConsecutiveDeclarations: AcrossEmptyLines
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
AllowShortFunctionsOnASingleLine: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
ColumnLimit: 120

8
include/sh4_core.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef SH4_CORE_H
#define SH4_CORE_H
#include <stdint.h>
#define __trapa(code) asm volatile("trapa %0" :: "i" (code))
#endif // SH4_CORE_H

View File

@ -3,6 +3,8 @@
#include <stdint.h>
#include "sh4_core.h"
#define __PACKED __attribute__((packed, aligned(1)))
#define __IO volatile

View File

@ -34,10 +34,12 @@ void uart_init(void) {
}
static void memory_test(void) {
printf("Begin memory test from 0x%08x to 0x%08x\r\n", MEMTEST_START, MEMTEST_END);
for (uint32_t i = MEMTEST_START; i < MEMTEST_END; i += 4) {
*(uint32_t *)i = i;
if (i % 0x10000 == 0U) {
if (i % 0x100000 == 0U) {
printf("Write to 0x%08lx...\r\n", i);
}
}
@ -49,10 +51,12 @@ static void memory_test(void) {
return;
}
if (i % 0x10000 == 0U) {
if (i % 0x100000 == 0U) {
printf("Read from 0x%08lx...\r\n", i);
}
}
printf("Memory test finished.\r\n");
}
int main(void) {
@ -66,16 +70,7 @@ int main(void) {
printf("Hello world\r\n");
printf("Size of int: %d\r\n", sizeof(int));
printf("Size of short: %d\r\n", sizeof(short));
printf("Size of char: %d\r\n", sizeof(char));
printf("Size of long: %d\r\n", sizeof(long));
printf("Size of pointer: %d\r\n", sizeof(uint8_t *));
printf("Size of uint8_t: %d\r\n", sizeof(uint8_t));
printf("Size of uint16_t: %d\r\n", sizeof(uint16_t));
printf("Size of uint32_t: %d\r\n", sizeof(uint32_t));
delay_ms(5000);
__trapa(34); /* TRAPA test code.. */
memory_test();
@ -88,3 +83,9 @@ int main(void) {
return 0;
}
/* tra #34 is SysCall.. */
int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
set_led(LED_RED_GPIO, LED_RED_PIN, 1U);
return 0;
}

View File

@ -1,8 +1,8 @@
#include "stx7105.h"
#define __WEAK __attribute__((weak))
#define __IRQ __attribute__((interrupt_handler))
#define __WEAK_IRQ __attribute__((weak, interrupt_handler))
#define WEAK_ATTR __attribute__((weak))
#define IRQ_ATTR __attribute__((interrupt_handler))
#define WEAK_IRQ_ATTR __attribute__((weak, interrupt_handler))
typedef enum {
EXP_TYPE_TRAP = 0x160,
@ -13,24 +13,50 @@ typedef enum {
INT_TYPE_TMU_TNUI1 = 0x420,
INT_TYPE_TMU_TNUI2 = 0x440,
INT_TYPE_TMU_TICPI2 = 0x460,
INT_TYPE_ASC_UART0 = 0x1160,
INT_TYPE_ASC_UART1 = 0x1140,
INT_TYPE_ASC_UART2 = 0x1120,
INT_TYPE_ASC_UART3 = 0x1100,
} intevt_type_t;
typedef enum {
TRA_TYPE_SYSCALL = 34,
} tra_type_t;
/* ========================= TMU 0/1/2 Underrun Interrupt Handlers ================================= */
__WEAK int tuni0_handler(void) {
WEAK_ATTR int tuni0_handler(void) {
/* Does nothing */
return 0;
}
__WEAK int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
WEAK_ATTR int tuni1_handler(void) {
/* Does nothing */
return 0;
}
__WEAK int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
tra_type_t tra = CSP->TRA;
WEAK_ATTR int tuni2_handler(void) {
/* Does nothing */
return 0;
}
/* ========================= ASC(UART) 0/1/2 Interrupt Handlers ================================= */
WEAK_ATTR int asc2_handler(void) {
/* Does nothing */
return 0;
}
/* ========================= Different Trap Code Handlers ================================= */
WEAK_ATTR int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
return 0;
}
/* ========================= TRAPA(Trap) Exception Handlers ================================= */
WEAK_ATTR int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
tra_type_t tra = (CSP->TRA >> 2U) & 0xFFU; /* TRA[9:2] */
switch (tra) {
case TRA_TYPE_SYSCALL:
@ -43,7 +69,9 @@ __WEAK int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
return 0;
}
__WEAK_IRQ int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
/* ========================= System Exception Handlers ================================= */
WEAK_IRQ_ATTR int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
expevt_type_t expevt = CSP->EXPEVT;
switch (expevt) {
case EXP_TYPE_TRAP:
@ -56,12 +84,23 @@ __WEAK_IRQ int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32
return 0;
}
__WEAK_IRQ int general_int_handler(void) {
/* ========================= System Interrupt Handlers ================================= */
WEAK_IRQ_ATTR int general_int_handler(void) {
intevt_type_t intevt = CSP->INTEVT;
switch (intevt) {
case INT_TYPE_TMU_TNUI0:
return tuni0_handler();
break;
case INT_TYPE_TMU_TNUI1:
return tuni1_handler();
break;
case INT_TYPE_TMU_TNUI2:
return tuni2_handler();
break;
case INT_TYPE_ASC_UART2:
return asc2_handler();
break;
default:
break;
}