sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
osd_config.c
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
17
18#include <tamtypes.h>
19#include <kernel.h>
20#include <stdio.h>
21#include <sys/fcntl.h>
22#include <sys/unistd.h>
23#include <string.h>
24#include <osd_config.h>
25#include <rom0_info.h>
26
28typedef struct
29{
30 s16 timezoneOffset;
31 u8 screenType;
32 u8 dateFormat;
33 u8 language;
34 u8 spdifMode;
35 u8 daylightSaving;
36 u8 timeFormat;
38
39extern ConfigParamT10K g_t10KConfig;
40
41ConfigParamT10K g_t10KConfig;
42
43__attribute__((constructor))
44static void __t10KConfigInitialize(void)
45{
46 g_t10KConfig.timezoneOffset = 540;
47 g_t10KConfig.screenType = TV_SCREEN_43;
48 g_t10KConfig.dateFormat = DATE_YYYYMMDD;
49 g_t10KConfig.language = LANGUAGE_JAPANESE;
50}
51
52static inline unsigned char tobcd(unsigned char dec)
53{
54 return dec + (dec / 10) * 6;
55}
56
57void converttobcd(sceCdCLOCK *time)
58{
59 time->second = tobcd(time->second);
60 time->minute = tobcd(time->minute);
61 time->hour = tobcd(time->hour);
62 time->day = tobcd(time->day);
63 time->month = tobcd(time->month);
64 time->year = tobcd(time->year);
65}
66
67static inline unsigned char frombcd(unsigned char bcd)
68{
69 return bcd - (bcd >> 4) * 6;
70}
71
72
73void convertfrombcd(sceCdCLOCK *time)
74{
75 time->second = frombcd(time->second);
76 time->minute = frombcd(time->minute);
77 time->hour = frombcd(time->hour);
78 time->day = frombcd(time->day);
79 time->month = frombcd(time->month);
80 time->year = frombcd(time->year);
81}
82
83static const unsigned char gDaysInMonths[12] = {
84 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
85
86static void adddate(sceCdCLOCK *time)
87{
88 // get the days in each month and fix up feb depending on leap year
89 unsigned char days_in_months[12];
90 memcpy(days_in_months, gDaysInMonths, 12);
91 if ((time->year & 3) == 0)
92 days_in_months[1] = 29;
93
94 // increment the day and check its within the "day of the month" bounds
95 time->day++;
96 if (time->day > days_in_months[time->month - 1]) {
97 time->day = 1;
98
99 // increment the month and check its within the "months in a year" bounds
100 time->month++;
101 if (time->month == 13) {
102 time->month = 1;
103
104 // check the year and increment it
105 time->year++;
106 if (time->year == 100) {
107 time->year = 0;
108 }
109 }
110 }
111}
112
113static void subdate(sceCdCLOCK *time)
114{
115 // get the days in each month and fix up feb depending on leap year
116 unsigned char days_in_months[12];
117 memcpy(days_in_months, gDaysInMonths, 12);
118 if ((time->year & 3) == 0)
119 days_in_months[1] = 29;
120
121 // decrement the day and check its within the "day of the month" bounds
122 time->day--;
123 if (time->day == 0) {
124 // decrement the month and check its within the "months in a year" bounds
125 time->month--;
126 if (time->month == 0) {
127 time->month = 12;
128
129 // check the year and decrement it
130 if (time->year == 0)
131 time->year = 99;
132 else
133 time->year--;
134 }
135
136 time->day = days_in_months[time->month - 1];
137 }
138}
139
140static void addhour(sceCdCLOCK *time)
141{
142 time->hour++;
143 if (time->hour == 24) {
144 adddate(time);
145 time->hour = 0;
146 }
147}
148
149static void subhour(sceCdCLOCK *time)
150{
151 if (time->hour == 0) {
152 subdate(time);
153 time->hour = 23;
154 } else
155 time->hour--;
156}
157
158void __adjustTime(sceCdCLOCK *time, int offset)
159{
160 convertfrombcd(time);
161 offset += time->minute;
162
163 if (offset >= 0) {
164 while (offset >= 60) {
165 addhour(time);
166 offset -= 60;
167 }
168 time->minute = offset;
169 } else {
170 while (offset < 0) {
171 subhour(time);
172 offset += 60;
173 }
174 time->minute = offset;
175 }
176
177 converttobcd(time);
178}
179
181{
182 return config.version == 0;
183}
184
186{
187 ConfigParam config;
188
189 if (IsT10K())
190 return g_t10KConfig.language;
191
192 GetOsdConfigParam(&config);
193 if (IsEarlyJap(config))
194 return config.japLanguage;
195 return config.language;
196}
197
198void configSetLanguage(int language)
199{
200 ConfigParam config;
201
202 // make sure language is valid
203 if (language < LANGUAGE_JAPANESE || language > LANGUAGE_PORTUGUESE)
204 return;
205 if (IsT10K())
206 g_t10KConfig.language = language;
207
208 // set language
209 GetOsdConfigParam(&config);
210 if (IsEarlyJap(config))
211 config.japLanguage = language;
212 else
213 config.language = language;
214 SetOsdConfigParam(&config);
215}
216
218{
219 ConfigParam config;
220
221 if (IsT10K())
222 return g_t10KConfig.screenType;
223
224 GetOsdConfigParam(&config);
225 return config.screenType;
226}
227
228void configSetTvScreenType(int screenType)
229{
230 ConfigParam config;
231
232 // make sure screen type is valid
233 if (screenType < TV_SCREEN_43 || screenType > TV_SCREEN_169)
234 return;
235 if (IsT10K())
236 g_t10KConfig.screenType = screenType;
237
238 // set screen type
239 GetOsdConfigParam(&config);
240 config.screenType = screenType;
241 SetOsdConfigParam(&config);
242}
243
245{
246 ConfigParam config;
247 Config2Param config2;
248
249 if (IsT10K())
250 return g_t10KConfig.dateFormat;
251
252 GetOsdConfigParam(&config);
253 if (IsEarlyJap(config))
254 return 0;
255 GetOsdConfigParam2(&config2, sizeof(config2), 0);
256 return config2.dateFormat;
257}
258
259void configSetDateFormat(int dateFormat)
260{
261 ConfigParam config;
262 Config2Param config2;
263
264 // make sure date format is valid
265 if (dateFormat < DATE_YYYYMMDD || dateFormat > DATE_DDMMYYYY)
266 return;
267 if (IsT10K())
268 g_t10KConfig.dateFormat = dateFormat;
269
270 // set date format
271 GetOsdConfigParam(&config);
272 if (IsEarlyJap(config))
273 return;
274 GetOsdConfigParam2(&config2, sizeof(config2), 0);
275 config2.dateFormat = dateFormat;
276 SetOsdConfigParam2(&config2, sizeof(config2), 0);
277}
278
280{
281 ConfigParam config;
282 Config2Param config2;
283
284 if (IsT10K())
285 return g_t10KConfig.timeFormat;
286
287 GetOsdConfigParam(&config);
288 if (IsEarlyJap(config))
289 return 0;
290 GetOsdConfigParam2(&config2, sizeof(config2), 0);
291 return config2.timeFormat;
292}
293
294void configSetTimeFormat(int timeFormat)
295{
296 ConfigParam config;
297 Config2Param config2;
298
299 // make sure time format is valid
300 if (timeFormat < TIME_24H || timeFormat > TIME_12H)
301 return;
302 if (IsT10K())
303 g_t10KConfig.timeFormat = timeFormat;
304
305 // set time format
306 GetOsdConfigParam(&config);
307 if (IsEarlyJap(config))
308 return;
309 GetOsdConfigParam2(&config2, sizeof(config2), 0);
310 config2.timeFormat = timeFormat;
311 SetOsdConfigParam2(&config2, sizeof(config2), 0);
312}
313
315{
316 ConfigParam config;
317 int timezoneOffset;
318
319 if (IsT10K())
320 {
321 timezoneOffset = g_t10KConfig.timezoneOffset;
322 }
323 else
324 {
325 GetOsdConfigParam(&config);
326 if (IsEarlyJap(config))
327 {
328 timezoneOffset = 540;
329 }
330 else
331 {
332 timezoneOffset = config.timezoneOffset;
333 // Check if this is negative, and manually make it positive using bit manipulation
334 if ((timezoneOffset & 0x400) != 0)
335 {
336 // Flip bits
337 timezoneOffset ^= 0x7ff;
338 // Add one
339 timezoneOffset += 1;
340 // Make it negative
341 timezoneOffset *= -1;
342 }
343 }
344 }
345 return timezoneOffset;
346}
347
348void configSetTimezone(int timezoneOffset)
349{
350 ConfigParam config;
351
352 // set offset from GMT
353 if (IsT10K())
354 g_t10KConfig.timezoneOffset = timezoneOffset;
355
356 GetOsdConfigParam(&config);
357 if (IsEarlyJap(config))
358 return;
359
360 {
361 u32 wantedTimezoneOffset;
362
363 // Reduce it to signed 11 bits if it is negative using bit manipulation
364 if (timezoneOffset < 0)
365 {
366 // Make it positive
367 wantedTimezoneOffset = -timezoneOffset;
368 // Subtract one
369 wantedTimezoneOffset -= 1;
370 // Flip bits
371 wantedTimezoneOffset ^= 0x7ff;
372 }
373 else
374 {
375 wantedTimezoneOffset = timezoneOffset;
376 }
377
378 config.timezoneOffset = wantedTimezoneOffset;
379 }
380
381 SetOsdConfigParam(&config);
382}
383
385{
386 ConfigParam config;
387
388 if (IsT10K())
389 return g_t10KConfig.spdifMode ^ 1;
390
391 GetOsdConfigParam(&config);
392 return config.spdifMode ^ 1;
393}
394
395void configSetSpdifEnabled(int enabled)
396{
397 ConfigParam config;
398
399 if (IsT10K())
400 g_t10KConfig.spdifMode = enabled ^ 1;
401
402 GetOsdConfigParam(&config);
403 config.spdifMode = enabled ^ 1;
404 SetOsdConfigParam(&config);
405}
406
408{
409 ConfigParam config;
410 Config2Param config2;
411
412 if (IsT10K())
413 return g_t10KConfig.daylightSaving;
414
415 GetOsdConfigParam(&config);
416 if (IsEarlyJap(config))
417 return 0;
418 GetOsdConfigParam2(&config2, sizeof(config2), 0);
419
420 return config2.daylightSaving;
421}
422
423void configSetDaylightSavingEnabled(int daylightSaving)
424{
425 ConfigParam config;
426 Config2Param config2;
427
428 if (IsT10K())
429 g_t10KConfig.daylightSaving = daylightSaving;
430
431 GetOsdConfigParam(&config);
432 if (IsEarlyJap(config))
433 return;
434 GetOsdConfigParam2(&config2, sizeof(config2), 0);
435 config2.daylightSaving = daylightSaving;
436 SetOsdConfigParam2(&config2, sizeof(config2), 0);
437}
438
440{
441 __adjustTime(time, -540);
442}
443
445{
446 int timezone_offset = configGetTimezone();
447 int daylight_saving = configIsDaylightSavingEnabled();
448 __adjustTime(time, timezone_offset - 540 + (daylight_saving * 60));
449}
int IsEarlyJap(ConfigParam config)
Definition osd_config.c:180
void configSetSpdifEnabled(int enabled)
Definition osd_config.c:395
int configGetTvScreenType(void)
Definition osd_config.c:217
void configSetTimeFormat(int timeFormat)
Definition osd_config.c:294
void configSetTimezone(int offset)
Definition osd_config.c:348
int configGetTimezone(void)
Definition osd_config.c:314
int configGetLanguage(void)
Definition osd_config.c:185
void configSetDateFormat(int dateFormat)
Definition osd_config.c:259
void configSetLanguage(int language)
Definition osd_config.c:198
int configGetDateFormat(void)
Definition osd_config.c:244
void configConvertToGmtTime(sceCdCLOCK *time)
Definition osd_config.c:439
int configIsSpdifEnabled(void)
Definition osd_config.c:384
void configSetDaylightSavingEnabled(int enabled)
Definition osd_config.c:423
int configGetTimeFormat(void)
Definition osd_config.c:279
void configSetTvScreenType(int screenType)
Definition osd_config.c:228
int configIsDaylightSavingEnabled(void)
Definition osd_config.c:407
void configConvertToLocalTime(sceCdCLOCK *time)
Definition osd_config.c:444
int IsT10K(void)
Definition rom0_info.c:81
u32 timezoneOffset
Definition osd_config.h:100
u32 japLanguage
Definition osd_config.h:92