summaryrefslogtreecommitdiff
path: root/packages/libndsfpc/examples/libmikmod/main7.pp
blob: 68e8f3657061841060a74063893542a54f73c73f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
87
88
89
90
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
program main7;

{$apptype arm7}
{$define ARM7} 

{$mode objfpc}

uses
  ctypes, nds7, mikmod7;
  
{ $include nds.inc} 
{ $include mikmod.inc}

procedure FIFOHandler();
var
  command: cuint32;
begin
	while (REG_IPC_FIFO_CR^ and IPC_FIFO_RECV_EMPTY) = 0 do
	begin
		command := REG_IPC_FIFO_RX^;
		if command >= (1 shl 28) then
			MikMod7_ProcessCommand(command);
		// process your own fifo messages here
	end;
end;

procedure startSound(sampleRate: cint; const data: pointer; bytes: cuint32; channel, vol, pan, format: cuint8);
var
  snd_format: integer;
begin
  if format = 1 then 
    snd_format := SOUND_8BIT 
  else 
    snd_format := SOUND_16BIT;
	SCHANNEL_TIMER(channel)^  := SOUND_FREQ(sampleRate);
	SCHANNEL_SOURCE(channel)^ := cuint32(data^);
	SCHANNEL_LENGTH(channel)^ := bytes shr 2;
	SCHANNEL_CR(channel)^     := SCHANNEL_ENABLE or SOUND_ONE_SHOT or SOUND_VOL(vol) or SOUND_PAN(pan) or (snd_format);
end;


function getFreeSoundChannel(): csint;
var	
  i: integer;
begin
	for i := 0 to 15 do
		if ((SCHANNEL_CR(i)^ and SCHANNEL_ENABLE)) = 0  then 
      result := i;
	result := -1;
end;

var
  vcount: integer;
  first, tempPos: touchPosition;
	lastbut: integer = -1;

procedure VcountHandler();
var
  but: integer;
  x, y, xpx, ypx, z1, z2: cuint16;
begin
  but := REG_KEYXY^;
  
  if (( (but xor lastbut) and (1 shl 6))) = 0 then
  begin 
    tempPos := touchReadXY();
    
    x := tempPos.x;
    y := tempPos.y;
    xpx := tempPos.px;
    ypx := tempPos.py;
    z1 := tempPos.z1;
    z2 := tempPos.z2;
  
  end else 
  begin
    lastbut := but;
    but := but or (1 shl 6);
  end;

  if ( vcount = 80 ) then
  begin
    first := tempPos;
  end else 
  begin
		if (abs(xpx - first.px) > 10) or (abs(ypx - first.py) > 10) or  ((but and (1 shl 6)) <> 0) then 
    begin
      but := but or (1 shl 6);
      lastbut := but;
    end else 
    begin
      IPC.mailBusy := 1;
      IPC.touchX := x;
      IPC.touchY := y;
      IPC.touchXpx := xpx;
      IPC.touchYpx := ypx;
      IPC.touchZ1 := z1;
      IPC.touchZ2 := z2;
      IPC.mailBusy := 0;
    end;
  end;
  IPC.buttons		:= but;
  vcount := vcount xor (80 xor 130);
  SetYtrigger(vcount);
end;

procedure VblankHandler();
var
	i: integer;
	snd: PTransferSound;
	chan: csint;
begin
	//sound code  :)
	snd := IPC.soundData;
	IPC.soundData := nil;

	if (snd <> nil) then
	begin
		for i := 0 to snd^.count - 1 do
		begin
			chan := getFreeSoundChannel();
			if (chan >= 0) then
			begin
				startSound(snd^.data[i].rate, snd^.data[i].data, snd^.data[i].len, chan, snd^.data[i].vol, snd^.data[i].pan, snd^.data[i].format);
			end;
		end;
	end;
end;

begin
  // init fifo
	REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_SEND_CLEAR;

	// Reset the clock if needed
	rtcReset();

	//enable sound
	powerON(POWER_SOUND);
	SOUND_CR^ := SOUND_ENABLE or SOUND_VOL($7F);
	IPC.soundData := nil;

	irqInit();
	irqSet(IRQ_VBLANK, @VblankHandler);
	irqEnable(IRQ_VBLANK);
	SetYtrigger(80);
	vcount := 80;
	irqSet(IRQ_VCOUNT, @VcountHandler);
	irqEnable(IRQ_VCOUNT);
	
	// Keep the ARM7 idle
	while true do
	begin
		FIFOHandler();
		swiWaitForVBlank();
	end;	
end.