example.cpp 2.65 KB
Newer Older
Erik Faye-Lund's avatar
Erik Faye-Lund committed
1
/* example.cpp -- Pimpmobile GameBoy Advance example
Erik Faye-Lund's avatar
Erik Faye-Lund committed
2
 * Copyright (C) 2005-2006 Jørn Nystad and Erik Faye-Lund
Erik Faye-Lund's avatar
Erik Faye-Lund committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * For conditions of distribution and use, see copyright notice in LICENSE.TXT
 */

#include <gba_console.h>
#include <gba_video.h>
#include <gba_interrupt.h>
#include <gba_systemcalls.h>
#include <gba_input.h>
#include <gba_timers.h>

#ifndef REG_WAITCNT
#define REG_WAITCNT (*(vu16*)(REG_BASE + 0x0204))
#endif

#include <stdio.h>
#include <assert.h>
#include <string.h>

21
#include <pimp_gba.h>
Erik Faye-Lund's avatar
Erik Faye-Lund committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "gbfs.h"

const void *mod = NULL;
GBFS_FILE const* fs;
const void *sample_bank = 0;
int curr_file = 0;
int file_count = 0;

int accum = 0;
void mix()
{
	REG_TM2CNT_L = 0;
	REG_TM2CNT_H = 0;
	REG_TM2CNT_H = TIMER_START;
36
37
	pimp_gba_vblank();
	pimp_gba_frame();
Erik Faye-Lund's avatar
Erik Faye-Lund committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	u32 value = REG_TM2CNT_L;
	accum += value;
}

void report()
{
	float percent = float(accum * 100) / (1 << 24);
	iprintf("cpu: %d.%03d%% (%d c/f)\n", int(percent), int(percent * 1000) % 1000, accum / 60);
	accum = 0;
}

void timer3()
{
	report();
}

void vblank()
{
	mix();
}

void callback(int type, int data)
{
	switch (type)
	{
		case PIMP_CALLBACK_SYNC:
		break;
		
		case PIMP_CALLBACK_UNSUPPORTED_EFFECT:
			iprintf("eff: %X\n", data);
		break;
		
		case PIMP_CALLBACK_UNSUPPORTED_VOLUME_EFFECT:
			iprintf("vol eff: %X\n", data);
		break;
	}
}

void play_next_file()
{
	static char name[32];

	do
	{
		mod = gbfs_get_nth_obj(fs, curr_file++, name, 0);
		if (curr_file > file_count - 1) curr_file = 0;
	}
	while (strncmp(name, "sample_bank.bin", 32) == 0);
	
87
	pimp_gba_close();
Erik Faye-Lund's avatar
Erik Faye-Lund committed
88
	iprintf("loading %s\n", name);
89
90
	pimp_gba_init((const pimp_module*)mod, sample_bank);
	pimp_gba_set_callback(callback);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
}

int main()
{
//	REG_WAITCNT = 0x46d6; // lets set some cool waitstates...
	REG_WAITCNT = 0x46da; // lets set some cool waitstates...
	
	irqInit();
	irqEnable(IRQ_VBLANK);
	consoleInit(0, 4, 0, NULL, 0, 15);

	BG_COLORS[0] = RGB5(0, 0, 0);
	BG_COLORS[241] = RGB5(31, 31, 31);
	REG_DISPCNT = MODE_0 | BG0_ON;
	
	fs = find_first_gbfs_file((void*)0x08000000);
	file_count = gbfs_count_objs(fs);
	sample_bank  = gbfs_get_obj(fs, "sample_bank.bin", 0);
	
	if (file_count <= 0)
	{
		iprintf("no files in file-system\n");
		return 1;
	}
	
	play_next_file();

	irqSet(IRQ_TIMER3, timer3);
	irqEnable(IRQ_TIMER3);
	REG_TM3CNT_L = 0;
	REG_TM3CNT_H = TIMER_START | TIMER_IRQ | 2;
	
	irqSet(IRQ_VBLANK, vblank);
	irqEnable(IRQ_VBLANK);
	
	while (1)
	{
		VBlankIntrWait();
		scanKeys();
		int keys = keysDown();
131
132
		if (keys & KEY_RIGHT) pimp_gba_set_pos(0, pimp_gba_get_order() + 1);
		if (keys & KEY_LEFT)  pimp_gba_set_pos(pimp_gba_get_row() + 8, pimp_gba_get_order());
Erik Faye-Lund's avatar
Erik Faye-Lund committed
133
134
135
		if (keys & KEY_A) play_next_file();
	}
	
136
	pimp_gba_close();
Erik Faye-Lund's avatar
Erik Faye-Lund committed
137
138
	return 0;
}