Subversion Repositories Tewi

Rev

Rev 189 | Rev 193 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 189 Rev 191
Line 1... Line 1...
1
/* $Id: main.c 189 2024-09-29 00:37:00Z nishi $ */
1
/* $Id: main.c 191 2024-09-29 05:11:20Z nishi $ */
2
 
2
 
3
#define SOURCE
3
#define SOURCE
4
 
4
 
5
#include "../config.h"
5
#include "../config.h"
6
 
6
 
Line 44... Line 44...
44
#include <debug.h>
44
#include <debug.h>
45
#include <sifrpc.h>
45
#include <sifrpc.h>
46
 
46
 
47
#define printf(...) scr_printf(__VA_ARGS__)
47
#define printf(...) scr_printf(__VA_ARGS__)
48
#define STDERR_LOG(...) scr_printf(__VA_ARGS__)
48
#define STDERR_LOG(...) scr_printf(__VA_ARGS__)
-
 
49
#elif defined(__PPU__)
-
 
50
#include <rsx/gcm_sys.h>
-
 
51
#include <rsx/rsx.h>
-
 
52
#include <sysutil/video.h>
-
 
53
#include <malloc.h>
-
 
54
#include <sys/thread.h>
-
 
55
#include <stdarg.h>
-
 
56
 
-
 
57
#define printf(...) tt_printf(__VA_ARGS__)
-
 
58
#define STDERR_LOG(...) tt_printf(__VA_ARGS__)
49
#else
59
#else
50
#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
60
#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
51
#endif
61
#endif
52
 
62
 
53
extern bool cm_do_log;
63
extern bool cm_do_log;
Line 113... Line 123...
113
	sceKernelSleepThreadCB();
123
	sceKernelSleepThreadCB();
114
	return 0;
124
	return 0;
115
}
125
}
116
#endif
126
#endif
117
 
127
 
-
 
128
#ifdef __PPU__
-
 
129
uint32_t depth_pitch;
-
 
130
uint32_t depth_offset;
-
 
131
uint32_t* depth_buffer;
-
 
132
 
-
 
133
#define CB_SIZE 0x100000
-
 
134
#define HOST_SIZE (32 * 1024 * 1024)
-
 
135
 
-
 
136
struct rsx_buffer {
-
 
137
	int width, height, id;
-
 
138
	uint32_t* ptr;
-
 
139
	uint32_t offset;
-
 
140
};
-
 
141
 
-
 
142
void wait_rsx(gcmContextData* ctx, uint32_t label) {
-
 
143
	rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
-
 
144
 
-
 
145
	rsxFlushBuffer(ctx);
-
 
146
 
-
 
147
	while(*(uint32_t*)gcmGetLabelAddress(GCM_INDEX_TYPE_32B) != label) usleep(50);
-
 
148
 
-
 
149
	label++;
-
 
150
}
-
 
151
 
-
 
152
void wait_rsx_until_idle(gcmContextData* ctx) {
-
 
153
	uint32_t label = 1;
-
 
154
	rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
-
 
155
	rsxSetWaitLabel(ctx, GCM_INDEX_TYPE_32B, label);
-
 
156
	label++;
-
 
157
	wait_rsx(ctx, label);
-
 
158
}
-
 
159
 
-
 
160
void get_resolution(int* width, int* height) {
-
 
161
	videoState state;
-
 
162
	videoResolution res;
-
 
163
	if(videoGetState(0, 0, &state) != 0) {
-
 
164
		return;
-
 
165
	}
-
 
166
 
-
 
167
	if(state.state != 0) {
-
 
168
		return;
-
 
169
	}
-
 
170
 
-
 
171
	if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
-
 
172
		return;
-
 
173
	}
-
 
174
	*width = res.width;
-
 
175
	*height = res.height;
-
 
176
}
-
 
177
 
-
 
178
void make_buffer(struct rsx_buffer* buffer, int id) {
-
 
179
	int w, h;
-
 
180
	get_resolution(&w, &h);
-
 
181
 
-
 
182
	buffer->ptr = (uint32_t*)rsxMemalign(64, 4 * w * h);
-
 
183
	if(buffer->ptr == NULL) return;
-
 
184
 
-
 
185
	if(rsxAddressToOffset(buffer->ptr, &buffer->offset) != 0) return;
-
 
186
 
-
 
187
	if(gcmSetDisplayBuffer(id, buffer->offset, 4 * w, w, h) != 0) return;
-
 
188
	buffer->width = w;
-
 
189
	buffer->height = h;
-
 
190
	buffer->id = id;
-
 
191
}
-
 
192
 
-
 
193
gcmContextData* init_screen(void) {
-
 
194
	void* host = memalign(1024 * 1024, HOST_SIZE);
-
 
195
	gcmContextData* ctx = NULL;
-
 
196
	videoState state;
-
 
197
	videoConfiguration vconfig;
-
 
198
	videoResolution res;
-
 
199
	rsxInit(&ctx, CB_SIZE, HOST_SIZE, host);
-
 
200
	if(ctx == NULL) {
-
 
201
		free(host);
-
 
202
		return NULL;
-
 
203
	}
-
 
204
 
-
 
205
	if(videoGetState(0, 0, &state) != 0) {
-
 
206
		rsxFinish(ctx, 0);
-
 
207
		free(host);
-
 
208
		return NULL;
-
 
209
	}
-
 
210
 
-
 
211
	if(state.state != 0) {
-
 
212
		rsxFinish(ctx, 0);
-
 
213
		free(host);
-
 
214
		return NULL;
-
 
215
	}
-
 
216
 
-
 
217
	if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
-
 
218
		rsxFinish(ctx, 0);
-
 
219
		free(host);
-
 
220
		return NULL;
-
 
221
	}
-
 
222
 
-
 
223
	memset(&vconfig, 0, sizeof(vconfig));
-
 
224
	vconfig.resolution = state.displayMode.resolution;
-
 
225
	vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
-
 
226
	vconfig.pitch = res.width * 4;
-
 
227
	vconfig.aspect = state.displayMode.aspect;
-
 
228
 
-
 
229
	wait_rsx_until_idle(ctx);
-
 
230
 
-
 
231
	if(videoConfigure(0, &vconfig, NULL, 0) != 0) {
-
 
232
		rsxFinish(ctx, 0);
-
 
233
		free(host);
-
 
234
		return NULL;
-
 
235
	}
-
 
236
 
-
 
237
	if(videoGetState(0, 0, &state) != 0) {
-
 
238
		rsxFinish(ctx, 0);
-
 
239
		free(host);
-
 
240
		return NULL;
-
 
241
	}
-
 
242
	gcmSetFlipMode(GCM_FLIP_VSYNC);
-
 
243
 
-
 
244
	depth_pitch = res.width * 4;
-
 
245
	depth_buffer = (uint32_t*)rsxMemalign(64, (res.height * depth_pitch) * 2);
-
 
246
	rsxAddressToOffset(depth_buffer, &depth_offset);
-
 
247
 
-
 
248
	gcmResetFlipStatus();
-
 
249
 
-
 
250
	return ctx;
-
 
251
}
-
 
252
 
-
 
253
void set_render_target(gcmContextData* context, struct rsx_buffer* buffer) {
-
 
254
	gcmSurface sf;
-
 
255
 
-
 
256
	sf.colorFormat = GCM_SURFACE_X8R8G8B8;
-
 
257
	sf.colorTarget = GCM_SURFACE_TARGET_0;
-
 
258
	sf.colorLocation[0] = GCM_LOCATION_RSX;
-
 
259
	sf.colorOffset[0] = buffer->offset;
-
 
260
	sf.colorPitch[0] = depth_pitch;
-
 
261
 
-
 
262
	sf.colorLocation[1] = GCM_LOCATION_RSX;
-
 
263
	sf.colorLocation[2] = GCM_LOCATION_RSX;
-
 
264
	sf.colorLocation[3] = GCM_LOCATION_RSX;
-
 
265
	sf.colorOffset[1] = 0;
-
 
266
	sf.colorOffset[2] = 0;
-
 
267
	sf.colorOffset[3] = 0;
-
 
268
	sf.colorPitch[1] = 64;
-
 
269
	sf.colorPitch[2] = 64;
-
 
270
	sf.colorPitch[3] = 64;
-
 
271
 
-
 
272
	sf.depthFormat = GCM_SURFACE_ZETA_Z16;
-
 
273
	sf.depthLocation = GCM_LOCATION_RSX;
-
 
274
	sf.depthOffset = depth_offset;
-
 
275
	sf.depthPitch = depth_pitch;
-
 
276
 
-
 
277
	sf.type = GCM_TEXTURE_LINEAR;
-
 
278
	sf.antiAlias = GCM_SURFACE_CENTER_1;
-
 
279
 
-
 
280
	sf.width = buffer->width;
-
 
281
	sf.height = buffer->height;
-
 
282
	sf.x = 0;
-
 
283
	sf.y = 0;
-
 
284
 
-
 
285
	rsxSetSurface(context, &sf);
-
 
286
}
-
 
287
 
-
 
288
void wait_flip(void) {
-
 
289
	while(gcmGetFlipStatus() != 0) usleep(200);
-
 
290
	gcmResetFlipStatus();
-
 
291
}
-
 
292
 
-
 
293
void flip(gcmContextData* ctx, uint32_t buffer) {
-
 
294
	if(gcmSetFlip(ctx, buffer) == 0) {
-
 
295
		rsxFlushBuffer(ctx);
-
 
296
		gcmSetWaitFlip(ctx);
-
 
297
	}
-
 
298
}
-
 
299
 
-
 
300
uint8_t* tvram;
-
 
301
 
-
 
302
extern uint8_t font[];
-
 
303
 
-
 
304
int tt_x = 0;
-
 
305
int tt_y = 0;
-
 
306
int tt_width;
-
 
307
int tt_height;
-
 
308
 
-
 
309
void tt_putstr(const char* str) {
-
 
310
	int i;
-
 
311
	for(i = 0; str[i] != 0; i++){
-
 
312
		tvram[tt_y * tt_width + tt_x] = str[i];
-
 
313
		if(str[i] == '\n'){
-
 
314
			tt_x = 0;
-
 
315
			tt_y++;
-
 
316
		}else{
-
 
317
			tt_x++;
-
 
318
			if(tt_x == tt_width){
-
 
319
				tt_x = 0;
-
 
320
				tt_y++;
-
 
321
			}
-
 
322
		}
-
 
323
		if(tt_y == tt_height){
-
 
324
			tt_y--;
-
 
325
			int x, y;
-
 
326
			for(y = 0; y < tt_height - 1; y++){
-
 
327
				for(x = 0; x < tt_width; x++){
-
 
328
					tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
-
 
329
				}
-
 
330
			}
-
 
331
			for(x = 0; x < tt_width; x++){
-
 
332
				tvram[(tt_height - 1) * tt_width + x] = 0;
-
 
333
			}
-
 
334
		}
-
 
335
	}
-
 
336
}
-
 
337
 
-
 
338
void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c){
-
 
339
	int i, j;
-
 
340
	if(c < 0x20) c = 0x20;
-
 
341
	if(c >= 0x7f) c = 0x20;
-
 
342
	for(i = 0; i < 7; i++){
-
 
343
		uint8_t l = font[(c - 0x20) * 8 + i];
-
 
344
		for(j = 0; j < 5; j++){
-
 
345
			uint32_t c = 0;
-
 
346
			if(l & (1 << 7)){
-
 
347
				c = 0xffffff;
-
 
348
			}
-
 
349
			l = l << 1;
-
 
350
			buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = c;
-
 
351
		}
-
 
352
	}
-
 
353
}
-
 
354
 
-
 
355
void draw(struct rsx_buffer* buffer, int current) {
-
 
356
	int i, j, c;
-
 
357
	for(i = 0; i < buffer->height / 8; i++) {
-
 
358
		for(j = 0; j < buffer->width / 6; j++) {
-
 
359
			uint8_t c = tvram[i * (buffer->width / 6) + j];
-
 
360
			tt_putchar(buffer, j, i, c);
-
 
361
		}
-
 
362
	}
-
 
363
}
-
 
364
 
-
 
365
#define BUFFERS 2
-
 
366
gcmContextData* ctx;
-
 
367
struct rsx_buffer buffers[BUFFERS];
-
 
368
 
-
 
369
void text_thread(void* arg) {
-
 
370
	int current = 0;
-
 
371
	while(1) {
-
 
372
		wait_flip();
-
 
373
		draw(&buffers[current], current);
-
 
374
		flip(ctx, buffers[current].id);
-
 
375
		current++;
-
 
376
		if(current >= BUFFERS) current = 0;
-
 
377
	}
-
 
378
}
-
 
379
 
-
 
380
void tt_printf(const char* tmpl, ...) {
-
 
381
	va_list va;
-
 
382
	va_start(va, tmpl);
-
 
383
	int i;
-
 
384
	char cbuf[2];
-
 
385
	cbuf[1] = 0;
-
 
386
	char* log = cm_strdup("");
-
 
387
	for(i = 0; tmpl[i] != 0; i++) {
-
 
388
		if(tmpl[i] == '%') {
-
 
389
			i++;
-
 
390
			if(tmpl[i] == 's'){
-
 
391
				char* tmp = log;
-
 
392
				log = cm_strcat(tmp, va_arg(va, char*));
-
 
393
				free(tmp);
-
 
394
			}else if(tmpl[i] == 'd'){
-
 
395
				char buf[513];
-
 
396
				sprintf(buf, "%d", va_arg(va, int));
-
 
397
				char* tmp = log;
-
 
398
				log = cm_strcat(tmp, buf);
-
 
399
				free(tmp);
-
 
400
			}else if(tmpl[i] == '%'){
-
 
401
				char* tmp = log;
-
 
402
				log = cm_strcat(tmp, "%");
-
 
403
				free(tmp);
-
 
404
			}
-
 
405
		} else {
-
 
406
			cbuf[0] = tmpl[i];
-
 
407
			char* tmp = log;
-
 
408
			log = cm_strcat(tmp, cbuf);
-
 
409
			free(tmp);
-
 
410
		}
-
 
411
	}
-
 
412
	va_end(va);
-
 
413
	tt_putstr(log);
-
 
414
}
-
 
415
 
-
 
416
#endif
-
 
417
 
118
int main(int argc, char** argv) {
418
int main(int argc, char** argv) {
119
	logfile = stderr;
419
	logfile = stderr;
120
#ifdef SERVICE
420
#ifdef SERVICE
121
	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
421
	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
122
	StartServiceCtrlDispatcher(table);
422
	StartServiceCtrlDispatcher(table);
Line 246... Line 546...
246
		while(running) sceKernelDelayThread(50 * 1000);
546
		while(running) sceKernelDelayThread(50 * 1000);
247
		sceKernelExitGame();
547
		sceKernelExitGame();
248
	}
548
	}
249
	printf("Connected, My IP is %s\n", info.ip);
549
	printf("Connected, My IP is %s\n", info.ip);
250
#elif defined(__PPU__)
550
#elif defined(__PPU__)
-
 
551
	int i;
-
 
552
	ctx = init_screen();
-
 
553
	int w, h;
-
 
554
	get_resolution(&w, &h);
-
 
555
	tt_width = w / 6;
-
 
556
	tt_height = h / 8;
-
 
557
	tvram = malloc((w / 6) * (h / 8));
-
 
558
	for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
-
 
559
	flip(ctx, BUFFERS - 1);
-
 
560
	sys_ppu_thread_t id;
-
 
561
	sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
251
	printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
562
	printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
252
	netInitialize();
563
	netInitialize();
253
#elif defined(__ps2sdk__)
564
#elif defined(__ps2sdk__)
254
	SifInitRpc(0);
565
	SifInitRpc(0);
255
	init_scr();
566
	init_scr();
Line 262... Line 573...
262
#ifdef _PSP
573
#ifdef _PSP
263
		printf("Error code %d\n", st);
574
		printf("Error code %d\n", st);
264
		while(running) sceKernelDelayThread(50 * 1000);
575
		while(running) sceKernelDelayThread(50 * 1000);
265
		sceKernelExitGame();
576
		sceKernelExitGame();
266
#else
577
#else
-
 
578
#ifdef __PPU__
-
 
579
		printf("Error code %d\n", st);
-
 
580
		while(1)
-
 
581
			;
-
 
582
#endif
267
		return st;
583
		return st;
268
#endif
584
#endif
269
	}
585
	}
270
	tw_server_loop();
586
	tw_server_loop();
271
#endif
587
#endif