main7.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. program main7;
  2. {$apptype arm7}
  3. {$define ARM7}
  4. {$mode objfpc}
  5. uses
  6. ctypes, nds7, dswifi7;
  7. { $include dswifi7.inc}
  8. procedure startSound(sampleRate: cint; const data: pointer; bytes: cuint32; channel, vol, pan, format: cuint8);
  9. var
  10. snd_format: integer;
  11. begin
  12. if format = 1 then
  13. snd_format := SOUND_8BIT
  14. else
  15. snd_format := SOUND_16BIT;
  16. SCHANNEL_TIMER(channel)^ := SOUND_FREQ(sampleRate);
  17. SCHANNEL_SOURCE(channel)^ := cuint32(data^);
  18. SCHANNEL_LENGTH(channel)^ := bytes shr 2;
  19. SCHANNEL_CR(channel)^ := SCHANNEL_ENABLE or SOUND_ONE_SHOT or SOUND_VOL(vol) or SOUND_PAN(pan) or (snd_format);
  20. end;
  21. function getFreeSoundChannel(): csint;
  22. var
  23. i: integer;
  24. begin
  25. for i := 0 to 15 do
  26. if ((SCHANNEL_CR(i)^ and SCHANNEL_ENABLE)) = 0 then
  27. result := i;
  28. result := -1;
  29. end;
  30. var
  31. vcount: integer;
  32. first, tempPos: touchPosition;
  33. lastbut: integer = -1;
  34. procedure VcountHandler();
  35. var
  36. but: integer;
  37. x, y, xpx, ypx, z1, z2: cuint16;
  38. begin
  39. but := REG_KEYXY^;
  40. if (( (but xor lastbut) and (1 shl 6))) = 0 then
  41. begin
  42. tempPos := touchReadXY();
  43. x := tempPos.x;
  44. y := tempPos.y;
  45. xpx := tempPos.px;
  46. ypx := tempPos.py;
  47. z1 := tempPos.z1;
  48. z2 := tempPos.z2;
  49. end else
  50. begin
  51. lastbut := but;
  52. but := but or (1 shl 6);
  53. end;
  54. if ( vcount = 80 ) then
  55. begin
  56. first := tempPos;
  57. end else
  58. begin
  59. if (abs(xpx - first.px) > 10) or (abs(ypx - first.py) > 10) or ((but and (1 shl 6)) <> 0) then
  60. begin
  61. but := but or (1 shl 6);
  62. lastbut := but;
  63. end else
  64. begin
  65. IPC.mailBusy := 1;
  66. IPC.touchX := x;
  67. IPC.touchY := y;
  68. IPC.touchXpx := xpx;
  69. IPC.touchYpx := ypx;
  70. IPC.touchZ1 := z1;
  71. IPC.touchZ2 := z2;
  72. IPC.mailBusy := 0;
  73. end;
  74. end;
  75. IPC.buttons := but;
  76. vcount := vcount xor (80 xor 130);
  77. SetYtrigger(vcount);
  78. end;
  79. procedure VblankHandler();
  80. var
  81. i: integer;
  82. snd: PTransferSound;
  83. chan: csint;
  84. begin
  85. //sound code :)
  86. snd := IPC.soundData;
  87. IPC.soundData := nil;
  88. if (snd <> nil) then
  89. begin
  90. for i := 0 to snd^.count - 1 do
  91. begin
  92. chan := getFreeSoundChannel();
  93. if (chan >= 0) then
  94. begin
  95. 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);
  96. end;
  97. end;
  98. end;
  99. Wifi_Update();
  100. end;
  101. procedure arm7_synctoarm9();
  102. begin
  103. // send fifo message
  104. REG_IPC_FIFO_TX^ := $87654321;
  105. end;
  106. procedure arm7_fifo();
  107. var
  108. msg: cuint32;
  109. begin
  110. // check incoming fifo messages
  111. msg := REG_IPC_FIFO_RX^;
  112. if (msg = $87654321) then
  113. Wifi_Sync();
  114. end;
  115. var
  116. fifo_temp: cuint32;
  117. begin
  118. REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_SEND_CLEAR;
  119. // Reset the clock if needed
  120. rtcReset();
  121. //enable sound
  122. powerON(POWER_SOUND);
  123. SOUND_CR^ := SOUND_ENABLE or SOUND_VOL($7F);
  124. IPC.soundData := nil;
  125. IPC.mailBusy := 0;
  126. irqInit();
  127. irqSet(IRQ_VBLANK, @VblankHandler);
  128. SetYtrigger(80);
  129. vcount := 80;
  130. irqSet(IRQ_VCOUNT, @VcountHandler);
  131. irqEnable(IRQ_VBLANK or IRQ_VCOUNT);
  132. irqSet(IRQ_WIFI, @Wifi_Interrupt);
  133. irqEnable(IRQ_WIFI);
  134. // trade some mail, to get a pointer from arm9
  135. while fifo_temp <> $12345678 do
  136. begin
  137. while (REG_IPC_FIFO_CR^ and IPC_FIFO_RECV_EMPTY) <> 0 do
  138. swiWaitForVBlank();
  139. fifo_temp := REG_IPC_FIFO_RX^;
  140. end;
  141. while (REG_IPC_FIFO_CR^ and IPC_FIFO_RECV_EMPTY) <> 0 do
  142. swiWaitForVBlank();
  143. fifo_temp := REG_IPC_FIFO_RX^;
  144. Wifi_Init(fifo_temp);
  145. irqSet(IRQ_FIFO_NOT_EMPTY, @arm7_fifo);
  146. irqEnable(IRQ_FIFO_NOT_EMPTY);
  147. REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_RECV_IRQ;
  148. Wifi_SetSyncHandler(@arm7_synctoarm9);
  149. // Keep the ARM7 idle
  150. while true do
  151. swiWaitForVBlank();
  152. end.