You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sound4Calc.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <fxlib.h>
  2. #include <stdlib.h>
  3. #include <addresses.h>
  4. #include <Sound4Calc.h>
  5. #include <syscall.h>
  6. //#define DEBUG
  7. //#define PI 3.14159265358
  8. unsigned char *itoa(int n, unsigned char* str, int base)
  9. {
  10. int i=1, j=0, x;
  11. if(n<0) str[j++] = '-', n = -n;
  12. for(x=n;x;x/=base) j++;
  13. for(x=n;x;x/=base) str[j-i++] = x%base + '0' + 39*(x%base>9);
  14. str[j] = 0;
  15. return str;
  16. }
  17. int main(void)
  18. {
  19. unsigned int key;
  20. // char buffer[50];
  21. unsigned char str[20];
  22. int sleep = 2000;
  23. int i;
  24. #ifdef DEBUG
  25. char before = 0, during = 0, after = 0;
  26. #endif
  27. setup();
  28. //ResetPin();
  29. while(1)
  30. {
  31. Bdisp_AllClr_VRAM();
  32. PrintMini(1, 1, itoa(sleep, str, 10), 0);
  33. PrintMini(1, 10, itoa(is_SH4, str, 10), 0);
  34. #ifdef DEBUG
  35. PrintMini(1, 20, itoa(before, str, 16), 0);
  36. PrintMini(1, 28, itoa(during, str, 16), 0);
  37. PrintMini(1, 36, itoa(after, str, 16), 0);
  38. #endif
  39. GetKey(&key);
  40. switch(key)
  41. {
  42. case KEY_CTRL_RIGHT : sleep+=50; break;
  43. case KEY_CTRL_LEFT : sleep-=50; break;
  44. case KEY_CTRL_EXE :
  45. while(Keyboard_KeyDown())
  46. {
  47. #ifdef DEBUG
  48. before=*(volatile unsigned char*)SH7305_PJDR;
  49. ResetPin();
  50. during=*(volatile unsigned char*)SH7305_PJDR;
  51. for(i=0;i<sleep;i++);
  52. SetPin();
  53. after=*(volatile unsigned char*)SH7305_PJDR;
  54. #else
  55. ResetPin();
  56. for(i=0;i<sleep;i++);
  57. SetPin();
  58. #endif
  59. }
  60. break;
  61. case KEY_CTRL_EXIT :
  62. return 1;
  63. }
  64. }
  65. return 1; // this point is never reached
  66. }
  67. void setup()
  68. {
  69. is_SH4 = getMPU();
  70. if(is_SH4)
  71. {
  72. // SCIF2 clock on (MSTPCR0.MSTP007)
  73. *(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080;
  74. // switch off SCSMR_2.TE and SCSMR_2.RE
  75. *(volatile unsigned short*)SH7305_SCSCR &= ~0x0030;
  76. // SCIF2 clock off (MSTPCR0.MSTP007)
  77. *(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080;
  78. // set bit 3 of port U to output mode
  79. *(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040;
  80. // set bit 4 and 5 of port U
  81. *(volatile unsigned char*)SH7305_PUDR |= 0x0C;
  82. // set port J bit 2 to output mode
  83. *(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x0030 ) | 0x0010;
  84. // set port J bit 3 to output mode
  85. *(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x00C0 ) | 0x0040;
  86. }
  87. else
  88. {
  89. // SCIF2 clock on (STBCR3.MSTP31)
  90. *(volatile unsigned char*)SH7337_STBCR3 &= ~0x02;
  91. // switch off SCSMR_2.TE and SCSMR_2.RE
  92. *(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030;
  93. // SCIF2 clock off (STBCR3.MSTP31)
  94. *(volatile unsigned char*)SH7337_STBCR3 |= 0x02;
  95. // set bit 6 of port G to output mode
  96. *(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000;
  97. // set bit 5 and 6 of port G
  98. *(volatile unsigned char*)SH7337_PGDR |= 0x60;
  99. // set port SC bit 0 to output
  100. *(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001;
  101. }
  102. /*
  103. // set port J bit 2 to output
  104. *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010;
  105. // set port J bit 3 to input
  106. *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080;*/
  107. }
  108. void SetPin()
  109. {
  110. if(is_SH4)
  111. {
  112. *(volatile unsigned char*)SH7305_PJDR |= 0x04;
  113. *(volatile unsigned char*)SH7305_PJDR &= ~0x08;
  114. //set pin to 0x4B
  115. }
  116. else
  117. {
  118. *(volatile unsigned char*)SH7337_SCPDR |= 0x01;
  119. }
  120. }
  121. void ResetPin()
  122. {
  123. if(is_SH4)
  124. {
  125. *(volatile unsigned char*)SH7305_PJDR &= ~0x04;
  126. *(volatile unsigned char*)SH7305_PJDR |= 0x08;
  127. // set the pin to 0x47
  128. }
  129. else
  130. {
  131. *(volatile unsigned char*)SH7337_SCPDR &= ~0x01;
  132. }
  133. }
  134. char getMPU(void)
  135. {
  136. // Port L control register.
  137. volatile unsigned short *plcr = (unsigned short *)0xA4000114;
  138. // Saved value for PLCR.
  139. unsigned short saved_plcr;
  140. unsigned int tested_plcr;
  141. saved_plcr = *plcr;
  142. *plcr = 0xffff;
  143. tested_plcr = *plcr;
  144. *plcr = saved_plcr;
  145. if(tested_plcr == 0x00ff || tested_plcr == 0x0fff)
  146. {
  147. return 0; // MPU_SH3
  148. }
  149. return 1; // MPU_SH4
  150. }