PS2SDK
PS2 Homebrew Libraries
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
16#include "internal.h"
17
18// Miscellaneous
19
21#define gif_chcr 0x1000a000
23#define gif_madr 0x1000a010
25#define gif_qwc 0x1000a020
26#define gif_tadr 0x1000a030
27
28 #define DMA_TAG_REFE 0x00
29 #define DMA_TAG_CNT 0x01
30 #define DMA_TAG_NEXT 0x02
31 #define DMA_TAG_REF 0x03
32 #define DMA_TAG_REFS 0x04
33 #define DMA_TAG_CALL 0x05
34 #define DMA_TAG_RET 0x06
35 #define DMA_TAG_END 0x07
36
37typedef struct {
39 unsigned direction :1;
41 unsigned pad1 :1;
43 unsigned mode :2;
45 unsigned asp :2;
47 unsigned tte :1;
49 unsigned tie :1;
51 unsigned start_flag :1;
53 unsigned pad2 :7;
55 unsigned tag :16;
57
58void GsDmaInit(void)
59{
60 /* This appears to have been based on code from Sony that initializes DMA channels 0-9, in bulk.
61 Reset/init DMA CH 2 (GIF) only. */
62 __asm__(
63 "li $2,0x1000A000 \n"
64 "sw $0,0x80($2) \n" // D2_SADR = 0. Documented to not exist, but is done.
65 "sw $0,0($2) \n" // D2_CHCR = 0
66 "sw $0,0x30($2) \n" // D2_TADR = 0
67 "sw $0,0x10($2) \n" // D2_MADR = 0
68 "sw $0,0x50($2) \n" // D2_ASR1 = 0
69 "sw $0,0x40($2) \n" // D2_ASR0 = 0
70 "li $2,0xFF1F \n" // Clear all interrupt status under D_STAT, other than SIF0, SIF1 & SIF2.
71 "sw $2,0x1000E010 \n"
72 "lw $2,0x1000E010 \n"
73 "lui $3,0xFF1F \n" // Clear all interrupt masks under D_STAT, other SIF0, SIF1 & SIF2. Writing a 1 reverses the bit.
74 "and $2,$3 \n"
75 "sw $2,0x1000E010 \n"
76 "sw $0,0x1000E000 \n" // D_CTRL = 0
77 "sw $0,0x1000E020 \n" // D_PCR = 0
78 "sw $0,0x1000E030 \n" // D_SQWC = 0
79 "sw $0,0x1000E050 \n" // D_RBOR = 0
80 "sw $0,0x1000E040 \n" // D_RBSR = 0
81 "li $3,1 \n"
82 "lw $2,0x1000E000 \n"
83 "ori $3,$2,1 \n" // D_CTRL (DMAE 1)
84 "sw $3,0x1000E000 \n"
85 );
86}
87
88void GsDmaSend(const void *addr, u32 qwords)
89{
90 DMA_CHCR chcr;
91 static char spr;
92
93 if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
94 {
95 spr = 1;
96 }
97 else
98 {
99 spr = 0;
100 }
101
102 *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;;
103
104 *((vu32 *)(gif_qwc)) = qwords;
105
106 chcr.direction =1;
107 chcr.mode =0;
108 chcr.asp =0;
109 chcr.tte =0;
110 chcr.tie =0;
111 chcr.start_flag =1;
112 chcr.tag =0;
113 chcr.pad1 =0;
114 chcr.pad2 =0;
115
116 // This prevents the compiler from assuming the values in addr are unused,
117 // and that the writes to addr can be delayed until after this function call
118 asm("":::"memory");
119
120 *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
121}
122
123void GsDmaSend_tag(const void *addr, u32 qwords, const GS_GIF_DMACHAIN_TAG *tag)
124{
125 DMA_CHCR chcr;
126 static char spr;
127
128 if((u32)addr >= 0x70000000 && (u32)addr <= 0x70003fff)
129 {
130 spr = 1;
131 }
132 else
133 {
134 spr = 0;
135 }
136
137 *((vu32 *)(gif_madr)) = ( u32 )((( u32 )addr) & 0x7FFFFFFF) << 0 | (u32)((spr) & 0x00000001) << 31;
138 *((vu32 *)(gif_qwc)) = qwords;
139 *((vu32 *)(gif_tadr)) = ( u32 )((( u32 )tag) & 0x7FFFFFFF) << 0 | (u32)((0) & 0x00000001) << 31;
140
141 chcr.direction =1;
142 chcr.mode =1; //chain
143 chcr.asp =0;
144 chcr.tte =0;
145 chcr.tie =0;
146 chcr.start_flag =1;
147 chcr.tag =0;
148 chcr.pad1 =0;
149 chcr.pad2 =0;
150
151 // This prevents the compiler from assuming the values in addr are unused,
152 // and that the writes to addr can be delayed until after this function call
153 asm("":::"memory");
154
155 *((volatile DMA_CHCR *)(gif_chcr)) = chcr;
156}
157
158void GsDmaWait(void)
159{
160 while(*((vu32 *)(0x1000a000)) & ((u32)1<<8));
161 asm("":::"memory");
162}
Definition dma.c:37
unsigned direction
Definition dma.c:39
unsigned tag
Definition dma.c:55
unsigned tte
Definition dma.c:47
unsigned pad1
Definition dma.c:41
unsigned tie
Definition dma.c:49
unsigned start_flag
Definition dma.c:51
unsigned mode
Definition dma.c:43
unsigned pad2
Definition dma.c:53
unsigned asp
Definition dma.c:45