PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
16#ifdef SIO2LOG
17
18#include <types.h>
19#include <defs.h>
20#include <stdio.h>
21#include <ioman.h>
22
23#include "log.h"
24
25#define DPRINTF(format, args...) printf("SIO2MAN: (%s): " format, __FUNCTION__ , ## args)
26
27#define FLUSH_COUNT_MAX 4
28#define DMA_MAX 1024
29
30static int init = 0;
31static int logging = 1;
32static int writesize = 0;
33static int flushcount = 0;
34static u8 log[10 * 1024];
35static int logfd = -1;
36
37static const char *logfile = "host0:sio2.log";
38
39static int log_init(void)
40{
41 logging = 1;
42
43 if ((logfd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC)) < 0)
44 logging = 0;
45
46 init = 1;
47 return 1;
48}
49
50void log_default(int type)
51{
52 if (!logging) return;
53
54 log_write8(type);
55}
56
57void log_portdata(u32 *pd1, u32 *pd2)
58{
59 int i;
60
61 if (!logging) return;
62
63 log_default(LOG_TRS_PD);
64
65 for (i = 0; i < 4; i++) {
66 log_write32(pd1[i]);
67 log_write32(pd2[i]);
68 }
69}
70
71void log_regdata(u32 *rd)
72{
73 int i;
74
75 if (!logging) return;
76
77 log_default(LOG_TRS_RD);
78
79 for (i = 0; i < 16; i++)
80 log_write32(rd[i]);
81}
82
83void log_data(int type, u8 *data, u32 size)
84{
85 int i;
86
87 if (!logging) return;
88
89 log_default(type);
90
91 log_write32(size);
92
93 for (i = 0; (u32)i < size; i++)
94 log_write8(data[i]);
95}
96
97void log_dma(int type, struct _sio2_dma_arg *arg)
98{
99 u8 *p;
100 int i, effective;
101
102 if (!logging) return;
103
104 log_default(type);
105
106 log_write32((u32)arg->addr);
107 log_write32(arg->size);
108 log_write32(arg->count);
109
110 effective = arg->size * 4 * arg->count;
111 if (effective > DMA_MAX)
112 effective = DMA_MAX;
113
114 for (i = 0, p = (u8 *)arg->addr; i < effective; i++)
115 log_write8(p[i]);
116}
117
118void log_stat(u32 stat6c, u32 stat70, u32 stat74)
119{
120 if (!logging) return;
121
122 log_default(LOG_TRR_STAT);
123
124 log_write32(stat6c); log_write32(stat70); log_write32(stat74);
125}
126
127void log_write8(u8 val)
128{
129 if (!init && !log_init())
130 return;
131
132 log[writesize++] = val;
133}
134
135void log_write32(u32 val)
136{
137 if (!init && !log_init())
138 return;
139
140 log[writesize] = val & 0xff;
141 log[writesize + 1] = (val >> 8) & 0xff;
142 log[writesize + 2] = (val >> 16) & 0xff;
143 log[writesize + 3] = (val >> 24) & 0xff;
144 writesize += 4;
145}
146
147void log_flush(int now)
148{
149 if (!init && !log_init())
150 return;
151
152 if (!logging || logfd < 0) return;
153
154 flushcount++;
155
156 if (now || flushcount >= FLUSH_COUNT_MAX) {
157 write(logfd, log, writesize);
158 close(logfd);
159 logfd = open(logfile, O_WRONLY | O_APPEND);
160 if (logfd < 0)
161 logging = 0;
162
163 writesize = 0;
164 }
165
166 if (flushcount >= FLUSH_COUNT_MAX)
167 flushcount = 0;
168}
169
170#endif