main7.pp 3.0 KB

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