PS2SDK
PS2 Homebrew Libraries
packets.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 QWORD GsPrimWorkArea[64] __attribute__((aligned(64))); // Align to a 64-byte boundary.
19 
20 QWORD *GsGifPacketsAlloc(GS_PACKET_TABLE *table, u32 num_qwords)
21 {
22  void *pointer;
23  GS_GIF_PACKET *packet;
24 
25  if(num_qwords <= (GS_PACKET_DATA_QWORD_MAX-table->qword_offset)) //check if we can alloc in current packet
26  {
27  pointer=&table->packets[table->packet_offset].data[table->qword_offset];
28  table->qword_offset += num_qwords;
29  }
30  else //couldnt fit so we going to have to try to jump to next packet
31  {
32  if(table->packet_offset+1 == table->packet_count) //no more packet available so we return error;
33  {
34  pointer=NULL;
35  }
36  else //there is another pocket available so we can jump to it
37  {
38  //before we jup to this packet then we got to end current packet and point it to the new one
39  packet=(GS_GIF_PACKET*)UNCACHED_SEG(&table->packets[table->packet_offset]);
40  packet->tag.qwc=table->qword_offset;
41  packet->tag.pad1=0;
42  packet->tag.pce=0;
43  packet->tag.id=0x02; //next = tag.addr
44  packet->tag.irq =0;
45  packet->tag.addr=(unsigned int)&((GS_GIF_PACKET*)UNCACHED_SEG(&table->packets[table->packet_offset + 1]))->tag;
46  packet->tag.spr =0;
47  packet->tag.pad2 =0;
48 
49  //now reset qwords offset in this packet & update packet offset
50  table->qword_offset= 0;
51  table->packet_offset++;
52 
53  //now we use the new packet
54  // but we still got to check if enough mem is available
55  if( num_qwords <= (GS_PACKET_DATA_QWORD_MAX-table->qword_offset) )
56  {
57  pointer=&table->packets[table->packet_offset].data[table->qword_offset];
58  table->qword_offset += num_qwords;
59  }
60  else //not enough
61  {
62  pointer=NULL;
63  }
64  }
65  }
66 
67  return pointer;
68 }
69 
70 void GsGifPacketsClear(GS_PACKET_TABLE *table)
71 {
72  table->packet_offset=0;
73  table->qword_offset=0;
74 }
75 
76 int GsGifPacketsExecute(GS_PACKET_TABLE *table, u16 wait)
77 {
78  GS_GIF_PACKET *packet;
79 
80  if(table->packet_offset==0 && table->qword_offset==0)
81  return 0;
82 
83  if(table->packets == NULL)
84  return -1;
85 
86  //close the current pocket
87  packet=(GS_GIF_PACKET*)UNCACHED_SEG(&table->packets[table->packet_offset]);
88  packet->tag.qwc =table->qword_offset;
89  packet->tag.pad1 =0;
90  packet->tag.pce =0;
91  packet->tag.id =0x07; //end
92  packet->tag.irq =0;
93  packet->tag.addr =(u32)0;
94  packet->tag.spr =0;
95  packet->tag.pad2 =0;
96 
97  GsDmaSend_tag(0, 0, &table->packets[0].tag);
98  if(wait)
99  GsDmaWait();
100 
101  return 0;
102 }
kernel.h
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
stdio.h
libgs.h
GS_PACKET_TABLE
Definition: libgs.h:2287
__attribute__
Definition: gif_registers.h:38
GS_GIF_PACKET
Definition: libgs.h:2281
QWORD
Definition: libgs.h:333
errno.h