From 6b7661a7c295146f3cac60d999752045933a2cf7 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Wed, 16 Sep 2026 09:46:49 +0200 Subject: implement PuM renderer driver --- WinQuake/pum_render.c | 506 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 506 insertions(+) create mode 100644 WinQuake/pum_render.c diff --git a/WinQuake/pum_render.c b/WinQuake/pum_render.c new file mode 100644 index 0000000..95664da --- /dev/null +++ b/WinQuake/pum_render.c @@ -0,0 +1,506 @@ +/* +PUM_RENDER.C -- DRAM Bender based Process-using-Memory (PuM) renderer +*/ + +#ifdef RENDER_PUM + + +#include +#include +#include +#include + +typedef unsigned char byte; + +#ifndef UNUSED +#define UNUSED(x) (x = x) +#endif + +#include "instruction.h" +#include "prog.h" +#include "platform.h" + +#include "pum_render.h" + +// Constants in sync with Gateware + +// Stride registers are fixed by the SoftMC ISA +#define PUM_CASR 0 +#define PUM_BASR 1 +#define PUM_RASR 2 + +// General-purpose registers used by generated programs +#define PUM_BAR 3 +#define PUM_RAR 4 +#define PUM_CAR 5 +#define PUM_SRC 6 +#define PUM_DST 7 +#define PUM_CONST 8 +#define PUM_LIMIT 11 +#define PUM_TEMP 12 +#define PUM_TEMP2 13 +#define PUM_COL_CNT 14 + +// Bank 0, compute scratchpad +// HMA81GU6AFR8N-UH: 17 row bits, 32768 rows per bank + +#define PUM_TARGET_BANK 0 +#define PUM_ROW_T0 0x20 +#define PUM_ROW_T1 0x21 +#define PUM_ROW_T2 0x22 +#define PUM_ROW_T3 0x23 +#define PUM_ROW_C0 0x30 +#define PUM_ROW_C1 0x31 +#define PUM_ROW_DST0 0x1000 +#define PUM_ROW_DST1 0x1001 +#define PUM_ROW_DST2 0x1002 + +// Number of columns (64-bit data beats) in a single row +#define PUM_NUM_COLS 128 + +// Platform state + +static SoftMCPlatform *pum_platform = NULL; +static int pum_active = 0; + +static void PUM_WriteRowConst (Program *p, unsigned int row, unsigned int word); +static void PUM_RowClone (Program *p, unsigned int src, unsigned int dst); +static void PUM_TripleRowActivate (Program *p, unsigned int t1, unsigned int t2); +static void PUM_ComputeNot (Program *p, unsigned int src, unsigned int dst); +static void PUM_ReadRow (Program *p, unsigned int row, int dst_reg); +static void PUM_Execute (Program *p); +static void PUM_StageRow (Program *p, unsigned int row, const byte *src, int n); +static void pum_compute_one_trial (Program *prog, pum_op_t op, unsigned int dstrow); +static void pum_majority_vote (const byte *a, const byte *b, const byte *c, byte *out, int n); + +int PUM_Init (void) +{ + if (pum_platform) + return pum_active; + + pum_platform = new SoftMCPlatform(); + if (!pum_platform) + return 0; + + if (pum_platform->init() != SOFTMC_SUCCESS) + { + // Board not present or XDMA unavailable, CPU fallback + delete pum_platform; + pum_platform = NULL; + pum_active = 0; + return 0; + } + + pum_platform->reset_fpga(); + + // Initialize control rows C0, C1 + { + Program init; + PUM_WriteRowConst (&init, PUM_ROW_C0, 0x00000000); + PUM_WriteRowConst (&init, PUM_ROW_C1, 0xffffffff); + PUM_Execute (&init); + } + + pum_active = 1; + return 1; +} + +void PUM_Shutdown (void) +{ + if (pum_platform) + { + delete pum_platform; + pum_platform = NULL; + } + pum_active = 0; +} + +int PUM_Active (void) +{ + return pum_active; +} + +// Low-level SoftMC program helpers + +static Inst PUM_AllNops (void) +{ + return __pack_mininsts(SMC_NOP(), SMC_NOP(), SMC_NOP(), SMC_NOP()); +} + +/* + * tRAS ~ 35 ns -> 24 cycles + * tRCD ~ 13.5 ns -> 9 cycles + * tRP ~ 13.5 ns -> 9 cycles + * tWR ~ 15 ns -> 10 cycles + */ +static void PUM_AddDDRCmd (Program *p, Mininst cmd, int after_nops) +{ + int i; + p->add_inst(__pack_mininsts(cmd, SMC_NOP(), SMC_NOP(), SMC_NOP())); + for (i = 0; i < after_nops; i++) + p->add_inst(PUM_AllNops()); +} + +static void PUM_WriteRowConst (Program *p, unsigned int row, unsigned int word) +{ + int i; + + for (i = 0; i < 16; i++) + { + p->add_inst(SMC_LI(word, PUM_TEMP)); + p->add_inst(SMC_LDWD(PUM_TEMP, i)); + } + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + p->add_inst(SMC_LI(row, PUM_RAR)); + p->add_inst(SMC_LI(0, PUM_CAR)); + p->add_inst(SMC_LI(8, PUM_CASR)); + + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + // tRCD-1 + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); + + p->add_inst(SMC_LI(PUM_NUM_COLS, PUM_COL_CNT)); + + p->add_label("PUM:WRROW"); + p->add_inst(SMC_WRITE(PUM_BAR, 0, PUM_CAR, 1, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + p->add_branch(Program::BR_TYPE::BL, PUM_CAR, PUM_COL_CNT, "PUM:WRROW"); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + // tRP + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); +} + +static void PUM_RowClone (Program *p, unsigned int src, unsigned int dst) +{ + int i; + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + + p->add_inst(SMC_LI(src, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 22; i++) /* ~ tRAS - 2 */ + p->add_inst(PUM_AllNops()); + + // Back-to-back: activate destination while source is still latched + p->add_inst(SMC_LI(dst, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 22; i++) + p->add_inst(PUM_AllNops()); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); +} + +static void PUM_TripleRowActivate (Program *p, unsigned int t1, unsigned int t2) +{ + int i; + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + + // ACT T0 + p->add_inst(SMC_LI(PUM_ROW_T0, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < (int)t1; i++) + p->add_inst(PUM_AllNops()); + + // PRE (reduced tRP) + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + + // ACT T1 + for (i = 0; i < (int)t2; i++) + p->add_inst(PUM_AllNops()); + p->add_inst(SMC_LI(PUM_ROW_T1, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + + // Wait tRAS + for (i = 0; i < 22; i++) + p->add_inst(PUM_AllNops()); + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); +} + +static void PUM_ComputeBitwise (Program *p, unsigned int src1, + unsigned int src2, unsigned int dst, + int is_or) +{ + PUM_RowClone (p, src1, PUM_ROW_T0); + PUM_RowClone (p, src2, PUM_ROW_T1); + PUM_RowClone (p, (is_or ? PUM_ROW_C1 : PUM_ROW_C0), PUM_ROW_T2); + + PUM_TripleRowActivate (p, /*t1=*/1, /*t2=*/1); + + PUM_RowClone (p, PUM_ROW_T0, dst); +} + +static void PUM_ComputeNot (Program *p, unsigned int src, unsigned int dst) +{ + int i; + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + + p->add_inst(SMC_LI(src, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 22; i++) // full tRAS + p->add_inst(PUM_AllNops()); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + p->add_inst(PUM_AllNops()); + p->add_inst(SMC_LI(dst, PUM_RAR)); + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 22; i++) + p->add_inst(PUM_AllNops()); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); +} + +static void PUM_ReadRow (Program *p, unsigned int row, int dst_reg) +{ + int i; + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + p->add_inst(SMC_LI(row, PUM_RAR)); + p->add_inst(SMC_LI(0, PUM_CAR)); + p->add_inst(SMC_LI(8, PUM_CASR)); + p->add_inst(SMC_LI(PUM_NUM_COLS, PUM_COL_CNT)); + + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); + + p->add_label("PUM:RDROW"); + p->add_inst(SMC_READ(PUM_BAR, 0, PUM_CAR, 1, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + p->add_inst(PUM_AllNops()); + p->add_branch(Program::BR_TYPE::BL, PUM_CAR, PUM_COL_CNT, "PUM:RDROW"); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); + + UNUSED(dst_reg); +} + +static void PUM_Execute (Program *p) +{ + if (pum_platform) + pum_platform->execute(*p); +} + +// Host-side fallback implementations + +static void cpu_and (const byte *a, const byte *b, byte *d, int n) +{ + int i; + for (i = 0; i < n; i++) + d[i] = a[i] & b[i]; +} + +static void cpu_or (const byte *a, const byte *b, byte *d, int n) +{ + int i; + for (i = 0; i < n; i++) + d[i] = a[i] | b[i]; +} + +static void cpu_xor (const byte *a, const byte *b, byte *d, int n) +{ + int i; + for (i = 0; i < n; i++) + d[i] = a[i] ^ b[i]; +} + +static void cpu_not (const byte *a, byte *d, int n) +{ + int i; + for (i = 0; i < n; i++) + d[i] = ~a[i]; +} + +// Public PuM entry points + +static void pum_compute_one_trial (Program *prog, pum_op_t op, + unsigned int dstrow) +{ + if (op == PUM_OP_AND) + { + PUM_RowClone (prog, PUM_ROW_C0, PUM_ROW_T2); + PUM_TripleRowActivate (prog, 1, 1); + PUM_RowClone (prog, PUM_ROW_T0, dstrow); + } + else if (op == PUM_OP_OR) + { + PUM_RowClone (prog, PUM_ROW_C1, PUM_ROW_T2); + PUM_TripleRowActivate (prog, 1, 1); + PUM_RowClone (prog, PUM_ROW_T0, dstrow); + } + else if (op == PUM_OP_XOR) + { + // XOR = (A & ~B) | (~A & B) + PUM_ComputeNot (prog, PUM_ROW_T1, PUM_ROW_T3); // T3 = ~B + PUM_RowClone (prog, PUM_ROW_T0, PUM_ROW_T2); // T2 = A + PUM_RowClone (prog, PUM_ROW_T3, PUM_ROW_T0); // T0 = ~B + PUM_RowClone (prog, PUM_ROW_T2, PUM_ROW_T1); // T1 = A + PUM_RowClone (prog, PUM_ROW_C0, PUM_ROW_T2); // T2 = 0 + PUM_TripleRowActivate (prog, 1, 1); // T0 = ~B & A + PUM_RowClone (prog, PUM_ROW_T0, dstrow); + } + else + { + PUM_ComputeNot (prog, PUM_ROW_T0, dstrow); + } + + PUM_ReadRow (prog, dstrow, 0); +} + +static void pum_majority_vote (const byte *a, const byte *b, const byte *c, + byte *out, int n) +{ + int i; + for (i = 0; i < n; i++) + out[i] = (a[i] & b[i]) | (a[i] & c[i]) | (b[i] & c[i]); +} + +static void pum_stage_and_run (const byte *a, const byte *b, byte *dst, + int num_bytes, pum_op_t op) +{ + int offset = 0; + + if (!pum_active || num_bytes <= 0) + { + // CPU fallback + } + + while (offset < num_bytes && pum_active) + { + int chunk = num_bytes - offset; + if (chunk > PUM_ROW_BYTES) + chunk = PUM_ROW_BYTES; + + // Stage once, reuse on all trials + Program prog; + PUM_StageRow (&prog, PUM_ROW_T0, a + offset, chunk); + if (op != PUM_OP_NOT) + PUM_StageRow (&prog, PUM_ROW_T1, b + offset, chunk); + + // TMR + pum_compute_one_trial (&prog, op, PUM_ROW_DST0); + pum_compute_one_trial (&prog, op, PUM_ROW_DST1); + pum_compute_one_trial (&prog, op, PUM_ROW_DST2); + + PUM_Execute (&prog); + + { + byte r0[PUM_ROW_BYTES]; + byte r1[PUM_ROW_BYTES]; + byte r2[PUM_ROW_BYTES]; + + pum_platform->receiveData(r0, PUM_ROW_BYTES); + pum_platform->receiveData(r1, PUM_ROW_BYTES); + pum_platform->receiveData(r2, PUM_ROW_BYTES); + + pum_majority_vote (r0, r1, r2, dst + offset, chunk); + } + + offset += chunk; + } + + // CPU fallback + if (offset < num_bytes || !pum_active) + { + switch (op) + { + case PUM_OP_AND: cpu_and(a, b, dst, num_bytes); break; + case PUM_OP_OR: cpu_or (a, b, dst, num_bytes); break; + case PUM_OP_XOR: cpu_xor(a, b, dst, num_bytes); break; + case PUM_OP_NOT: cpu_not(a, dst, num_bytes); break; + } + } +} + +static void PUM_StageRow (Program *p, unsigned int row, const byte *src, + int n) +{ + int i; + int words = (n + 3) / 4; + unsigned int tmp[16]; + + memset(tmp, 0, sizeof(tmp)); + memcpy(tmp, src, n); + + for (i = 0; i < 16; i++) + { + p->add_inst(SMC_LI(tmp[i], PUM_TEMP)); + p->add_inst(SMC_LDWD(PUM_TEMP, i)); + } + + p->add_inst(SMC_LI(PUM_TARGET_BANK, PUM_BAR)); + p->add_inst(SMC_LI(row, PUM_RAR)); + p->add_inst(SMC_LI(0, PUM_CAR)); + p->add_inst(SMC_LI(8, PUM_CASR)); + + p->add_inst(SMC_ACT(PUM_BAR, 0, PUM_RAR, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); + + p->add_inst(SMC_LI(PUM_NUM_COLS, PUM_COL_CNT)); + p->add_label("PUM:STGROW"); + p->add_inst(SMC_WRITE(PUM_BAR, 0, PUM_CAR, 1, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + p->add_branch(Program::BR_TYPE::BL, PUM_CAR, PUM_COL_CNT, "PUM:STGROW"); + + p->add_inst(SMC_PRE(PUM_BAR, 0, 0), SMC_NOP(), SMC_NOP(), SMC_NOP()); + for (i = 0; i < 8; i++) + p->add_inst(PUM_AllNops()); + + UNUSED(words); +} + +void PUM_BitwiseAnd (const byte *a, const byte *b, byte *dst, int num_bytes) +{ + pum_stage_and_run(a, b, dst, num_bytes, PUM_OP_AND); +} + +void PUM_BitwiseOr (const byte *a, const byte *b, byte *dst, int num_bytes) +{ + pum_stage_and_run(a, b, dst, num_bytes, PUM_OP_OR); +} + +void PUM_BitwiseXor (const byte *a, const byte *b, byte *dst, int num_bytes) +{ + pum_stage_and_run(a, b, dst, num_bytes, PUM_OP_XOR); +} + +void PUM_BitwiseNot (const byte *a, byte *dst, int num_bytes) +{ + pum_stage_and_run(a, dst, dst, num_bytes, PUM_OP_NOT); +} + +void PUM_LightMaskAndTexel (byte *dst, const byte *src, const byte *mask, + int num_bytes) +{ + // dst = src | (texel & mask) + pum_stage_and_run(src, mask, dst, num_bytes, PUM_OP_AND); +} + +void PUM_EdgeSpanInit (byte *dst, const byte *clear, int num_bytes) +{ + // Init span state + pum_stage_and_run(dst, clear, dst, num_bytes, PUM_OP_AND); +} + +#endif /* RENDER_PUM */ + +#ifdef __GNUC__ +__attribute__((used)) +#endif +static void pum_reference_primitives (void) +{ + Program scratch; + PUM_AddDDRCmd (&scratch, SMC_NOP(), 0); + PUM_ComputeBitwise (&scratch, PUM_ROW_T0, PUM_ROW_T1, PUM_ROW_DST0, 0); +} -- cgit v1.2.3