sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
dma.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# (c) 2009 Lion
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include <errno.h>
12#include <stdio.h>
13#include <kernel.h>
14#include <libgs.h>
15#include <ee_regs.h>
16
17#include "internal.h"
18
19// Miscellaneous
20
22#define gif_chcr 0x1000a000
24#define gif_madr 0x1000a010
26#define gif_qwc 0x1000a020
27#define gif_tadr 0x1000a030
28
29 #define DMA_TAG_REFE 0x00
30 #define DMA_TAG_CNT 0x01
31 #define DMA_TAG_NEXT 0x02
32 #define DMA_TAG_REF 0x03
33 #define DMA_TAG_REFS 0x04
34 #define DMA_TAG_CALL 0x05
35 #define DMA_TAG_RET 0x06
36 #define DMA_TAG_END 0x07
37
38typedef struct {
40 unsigned direction :1;
42 unsigned pad1 :1;
44 unsigned mode :2;
46 unsigned asp :2;
48 unsigned tte :1;
50 unsigned tie :1;
52 unsigned start_flag :1;
54 unsigned pad2 :7;
56 unsigned tag :16;
58
59void GsDmaInit(void)
60{
61 /* This appears to have been based on code from Sony that initializes DMA channels 0-9, in bulk.
62 Reset/init DMA CH 2 (GIF) only. */
63 *((vu32 *)0x1000A080) = 0; // D2_SADR = 0. Documented to not exist, but is done.
64 *R_EE_D2_CHCR = 0;
65 *R_EE_D2_TADR = 0;
66 *R_EE_D2_MADR = 0;
67 *R_EE_D2_ASR1 = 0;
68 *R_EE_D2_ASR0 = 0;
69 *R_EE_D_STAT = 0xFF1F; // Clear all interrupt status under D_STAT, other than SIF0, SIF1 & SIF2.
70 *R_EE_D_STAT &= 0xFF1F << 16; // Clear all interrupt masks under D_STAT, other SIF0, SIF1 & SIF2. Writing a 1 reverses the bit.
71 *R_EE_D_CTRL = 0;
72 *R_EE_D_PCR = 0;
73 *R_EE_D_SQWC = 0;
74 *R_EE_D_RBOR = 0;
75 *R_EE_D_RBSR = 0;
76 *R_EE_D_CTRL |= 1; // (DMAE 1)
77}
78
79void GsDmaSend(const void *addr, u32 qwords)
80{
81 DMA_CHCR chcr;
82 static char spr;
83
84 if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
85 {
86 spr = 1;
87 }
88 else
89 {
90 spr = 0;
91 }
92
93 *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;;
94
95 *((vu32 *)(gif_qwc)) = qwords;
96
97 chcr.direction =1;
98 chcr.mode =0;
99 chcr.asp =0;
100 chcr.tte =0;
101 chcr.tie =0;
102 chcr.start_flag =1;
103 chcr.tag =0;
104 chcr.pad1 =0;
105 chcr.pad2 =0;
106
107 // This prevents the compiler from assuming the values in addr are unused,
108 // and that the writes to addr can be delayed until after this function call
109 __asm__ ("":::"memory");
110
111 *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
112}
113
114void GsDmaSend_tag(const void *addr, u32 qwords, const GS_GIF_DMACHAIN_TAG *tag)
115{
116 DMA_CHCR chcr;
117 static char spr;
118
119 if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
120 {
121 spr = 1;
122 }
123 else
124 {
125 spr = 0;
126 }
127
128 *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;
129 *((vu32 *)(gif_qwc)) = qwords;
130 *((vu32 *)(gif_tadr)) = ( u32 )((( u32 )tag) & 0x7FFFFFFF) << 0 | (u32)((0) & 0x00000001) << 31;
131
132 chcr.direction =1;
133 chcr.mode =1; //chain
134 chcr.asp =0;
135 chcr.tte =0;
136 chcr.tie =0;
137 chcr.start_flag =1;
138 chcr.tag =0;
139 chcr.pad1 =0;
140 chcr.pad2 =0;
141
142 // This prevents the compiler from assuming the values in addr are unused,
143 // and that the writes to addr can be delayed until after this function call
144 __asm__ ("":::"memory");
145
146 *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
147}
148
149void GsDmaWait(void)
150{
151 while(*R_EE_D2_CHCR & ((u32)1<<8));
152 __asm__ ("":::"memory");
153}
#define R_EE_D_CTRL
Definition ee_regs.h:449
#define R_EE_D2_CHCR
Definition ee_regs.h:385
#define R_EE_D2_ASR0
Definition ee_regs.h:393
#define R_EE_D2_TADR
Definition ee_regs.h:391
#define R_EE_D_RBSR
Definition ee_regs.h:457
#define R_EE_D2_ASR1
Definition ee_regs.h:395
#define R_EE_D_SQWC
Definition ee_regs.h:455
#define R_EE_D_PCR
Definition ee_regs.h:453
#define R_EE_D_RBOR
Definition ee_regs.h:459
#define R_EE_D_STAT
Definition ee_regs.h:451
#define R_EE_D2_MADR
Definition ee_regs.h:387
Definition dma.c:38
unsigned direction
Definition dma.c:40
unsigned tag
Definition dma.c:56
unsigned tte
Definition dma.c:48
unsigned pad1
Definition dma.c:42
unsigned tie
Definition dma.c:50
unsigned start_flag
Definition dma.c:52
unsigned mode
Definition dma.c:44
unsigned pad2
Definition dma.c:54
unsigned asp
Definition dma.c:46