Optimize asm matmul dma software path
This commit is contained in:
@@ -1,42 +1,98 @@
|
||||
/**
|
||||
* user-sample - CIC-IEC 2026 Stage 3 Evaluation Program
|
||||
*
|
||||
* Matmul AXI-master path.
|
||||
* Matmul AXI-master path with fast startup and low-overhead UART output.
|
||||
*/
|
||||
|
||||
#include <dma.h>
|
||||
|
||||
/* ─── UART 16550 direct access (bypasses slow printf/picolibc) ──────── */
|
||||
#define UART_THR (*(volatile unsigned char *)0xbf000000)
|
||||
#define UART_LSR (*(volatile unsigned char *)0xbf000005)
|
||||
#define LSR_THRE 0x20
|
||||
|
||||
static inline void uart_tx(unsigned char c) {
|
||||
while (!(UART_LSR & LSR_THRE)) {}
|
||||
static inline void uart_tx(unsigned char c)
|
||||
{
|
||||
while ((UART_LSR & LSR_THRE) == 0) {
|
||||
}
|
||||
UART_THR = c;
|
||||
}
|
||||
|
||||
static void uart_puts(const char *s) {
|
||||
while (*s) uart_tx(*s++);
|
||||
static void uart_put_matmul_start_fifo(void)
|
||||
{
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'A';
|
||||
UART_THR = 'T';
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'U';
|
||||
UART_THR = 'L';
|
||||
UART_THR = '_';
|
||||
UART_THR = 'S';
|
||||
UART_THR = 'T';
|
||||
UART_THR = 'A';
|
||||
UART_THR = 'R';
|
||||
UART_THR = 'T';
|
||||
}
|
||||
|
||||
static void uart_puthex(unsigned int val) {
|
||||
int i;
|
||||
for (i = 28; i >= 0; i -= 4) {
|
||||
unsigned char nibble = (val >> i) & 0xF;
|
||||
uart_tx(nibble < 10 ? '0' + nibble : 'a' + nibble - 10);
|
||||
static inline void uart_wait_tx_empty(void)
|
||||
{
|
||||
while ((UART_LSR & LSR_THRE) == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ─── Configurable group count (default 5000) ──────────────────────── */
|
||||
static inline unsigned char hex_digit(unsigned int val, int shift)
|
||||
{
|
||||
unsigned char nibble = (unsigned char)((val >> shift) & 0xf);
|
||||
|
||||
return (unsigned char)(nibble < 10 ? ('0' + nibble) : ('a' + nibble - 10));
|
||||
}
|
||||
|
||||
static void uart_put_result_fifo(unsigned int crc)
|
||||
{
|
||||
uart_wait_tx_empty();
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'A';
|
||||
UART_THR = 'T';
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'U';
|
||||
UART_THR = 'L';
|
||||
UART_THR = '_';
|
||||
UART_THR = 'C';
|
||||
UART_THR = 'R';
|
||||
UART_THR = 'C';
|
||||
UART_THR = '3';
|
||||
UART_THR = '2';
|
||||
UART_THR = '=';
|
||||
UART_THR = hex_digit(crc, 28);
|
||||
UART_THR = hex_digit(crc, 24);
|
||||
UART_THR = hex_digit(crc, 20);
|
||||
|
||||
uart_wait_tx_empty();
|
||||
UART_THR = hex_digit(crc, 16);
|
||||
UART_THR = hex_digit(crc, 12);
|
||||
UART_THR = hex_digit(crc, 8);
|
||||
UART_THR = hex_digit(crc, 4);
|
||||
UART_THR = hex_digit(crc, 0);
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'A';
|
||||
UART_THR = 'T';
|
||||
UART_THR = 'M';
|
||||
UART_THR = 'U';
|
||||
UART_THR = 'L';
|
||||
UART_THR = '_';
|
||||
UART_THR = 'D';
|
||||
UART_THR = 'O';
|
||||
UART_THR = 'N';
|
||||
UART_THR = 'E';
|
||||
}
|
||||
|
||||
#ifndef MATMUL_GROUP_NUM
|
||||
#define MATMUL_GROUP_NUM 5000
|
||||
#endif
|
||||
|
||||
/* ─── BSP globals ─────────────────────────────────────────────────── */
|
||||
#if MATMUL_FAST_START == 0
|
||||
unsigned long UART_BASE = 0xbf000000;
|
||||
#endif
|
||||
|
||||
/* ─── Memory layout ────────────────────────────────────────────────── */
|
||||
#define EXTRAM_BASE_PHYS 0x1c400000
|
||||
#define EXTRAM_BASE_UNCACHED (0xA0000000 | (EXTRAM_BASE_PHYS & 0x1FFFFFFF))
|
||||
|
||||
@@ -44,11 +100,9 @@ unsigned long UART_BASE = 0xbf000000;
|
||||
#define DST_BASE_WORDS (SRC_BASE_WORDS + MATMUL_GROUP_NUM * 32)
|
||||
#define RESULT_WORDS (MATMUL_GROUP_NUM * 48)
|
||||
|
||||
/* ExtRAM Physical Addresses for DMA */
|
||||
#define EXTRAM_PHYS_AB(g) (EXTRAM_BASE_PHYS + (SRC_BASE_WORDS * 4) + (g) * 128)
|
||||
#define EXTRAM_PHYS_C(g) (EXTRAM_BASE_PHYS + (DST_BASE_WORDS * 4) + (g) * 192)
|
||||
|
||||
/* ─── MATMUL IP registers (Ping-Pong Memory Map) ──────────────────── */
|
||||
#define MATMUL_BASE_PHYS 0x1f500000
|
||||
#define MATMUL_BASE_UNCACHED 0xbf500000
|
||||
|
||||
@@ -70,24 +124,22 @@ unsigned long UART_BASE = 0xbf000000;
|
||||
|
||||
#define MATMUL_CRC (MATMUL_BASE_UNCACHED + 0x010)
|
||||
|
||||
/* ─── Pre-computed DMA burst control word ──────────────────────────── */
|
||||
#define DMA_CTRL_VAL (CTRL_BURST_SIZE(2) | CTRL_BURST_LEN(63) | CTRL_START_BIT)
|
||||
#define MATMUL_EP_ID 0
|
||||
|
||||
/* ═══════════════════════════════════════════════════════════════════ */
|
||||
int main(void)
|
||||
{
|
||||
if (MATMUL_GROUP_NUM <= 0) return 0;
|
||||
if (MATMUL_GROUP_NUM <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── Hot-path local volatile pointers (one-time addr calc) ──────── */
|
||||
volatile unsigned int *mat_ctrl = (volatile unsigned int *)MATMUL_CTRL;
|
||||
volatile unsigned int *mat_status = (volatile unsigned int *)MATMUL_STATUS;
|
||||
volatile unsigned int *mat_crc = (volatile unsigned int *)MATMUL_CRC;
|
||||
|
||||
unsigned int crc;
|
||||
|
||||
/* ── Start marker (must be first output) ──────────────────── */
|
||||
uart_puts("MATMUL_START\n");
|
||||
uart_put_matmul_start_fifo();
|
||||
|
||||
*(volatile unsigned int *)MATMUL_CRC = 0;
|
||||
*mat_status = STATUS_DMA_DONE | STATUS_DONE | STATUS_ERROR;
|
||||
@@ -100,12 +152,9 @@ int main(void)
|
||||
while (!(*mat_status & STATUS_DMA_DONE)) {}
|
||||
*mat_status = STATUS_DMA_DONE;
|
||||
|
||||
/* ── Read hardware CRC32 ──────────────────────────────────── */
|
||||
crc = *mat_crc;
|
||||
|
||||
uart_puts("MATMUL_CRC32=");
|
||||
uart_puthex(crc);
|
||||
uart_puts("\nMATMUL_DONE\n");
|
||||
uart_put_result_fifo(crc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user