Sound4Calc/src/Sound4Calc.c

183 lines
4.5 KiB
C
Raw Normal View History

2016-05-15 14:00:40 +02:00
#include <fxlib.h>
2016-05-14 22:19:51 +02:00
#include <stdlib.h>
2016-05-15 14:00:40 +02:00
#include <addresses.h>
#include <Sound4Calc.h>
2016-05-14 22:19:51 +02:00
2016-05-15 14:00:40 +02:00
#include <syscall.h>
2016-05-14 22:19:51 +02:00
//#define DEBUG
#define PI 3.14159265358
2016-05-16 13:32:58 +02:00
unsigned char *itoa(int n, unsigned char* str, int base)
2016-05-14 22:19:51 +02:00
{
int i=1, j=0, x;
if(n<0) str[j++] = '-', n = -n;
for(x=n;x;x/=base) j++;
for(x=n;x;x/=base) str[j-i++] = x%base + '0' + 39*(x%base>9);
str[j] = 0;
return str;
}
int main(void)
{
unsigned int key;
char buffer[50];
unsigned char str[20];
int sleep = 2000;
int i;
2016-05-16 13:32:58 +02:00
#ifdef DEBUG
2016-05-14 22:19:51 +02:00
char before = 0, during = 0, after = 0;
2016-05-16 13:32:58 +02:00
#endif
2016-05-14 22:19:51 +02:00
setup();
//ResetPin();
while(1)
{
Bdisp_AllClr_VRAM();
PrintMini(1, 1, itoa(sleep, str, 10), 0);
PrintMini(1, 10, itoa(is_SH4, str, 10), 0);
#ifdef DEBUG
PrintMini(1, 20, itoa(before, str, 16), 0);
PrintMini(1, 28, itoa(during, str, 16), 0);
PrintMini(1, 36, itoa(after, str, 16), 0);
#endif
GetKey(&key);
switch(key)
{
case KEY_CTRL_RIGHT : sleep+=50; break;
case KEY_CTRL_LEFT : sleep-=50; break;
case KEY_CTRL_EXE :
while(Keyboard_KeyDown())
{
#ifdef DEBUG
2016-05-16 13:32:58 +02:00
before=*(volatile unsigned char*)SH7305_PJDR;
2016-05-14 22:19:51 +02:00
ResetPin();
2016-05-16 13:32:58 +02:00
during=*(volatile unsigned char*)SH7305_PJDR;
2016-05-14 22:19:51 +02:00
for(i=0;i<sleep;i++);
SetPin();
2016-05-16 13:32:58 +02:00
after=*(volatile unsigned char*)SH7305_PJDR;
2016-05-14 22:19:51 +02:00
#else
ResetPin();
for(i=0;i<sleep;i++);
SetPin();
#endif
}
break;
case KEY_CTRL_EXIT :
return 1;
}
}
return 1; // this point is never reached
}
void setup()
{
is_SH4 = getMPU();
if(is_SH4)
{
// SCIF2 clock on (MSTPCR0.MSTP007)
2016-05-16 13:32:58 +02:00
*(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080;
2016-05-14 22:19:51 +02:00
// switch off SCSMR_2.TE and SCSMR_2.RE
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7305_SCSCR &= ~0x0030;
2016-05-14 22:19:51 +02:00
// SCIF2 clock off (MSTPCR0.MSTP007)
2016-05-16 13:32:58 +02:00
*(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080;
2016-05-14 22:19:51 +02:00
// set bit 3 of port U to output mode
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040;
2016-05-14 22:19:51 +02:00
// set bit 4 and 5 of port U
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7305_PUDR |= 0x0C;
2016-05-14 22:19:51 +02:00
// set port J bit 2 to output mode
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x0030 ) | 0x0010;
2016-05-14 22:19:51 +02:00
// set port J bit 3 to output mode
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x00C0 ) | 0x0040;
2016-05-14 22:19:51 +02:00
}
else
{
// SCIF2 clock on (STBCR3.MSTP31)
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7337_STBCR3 &= ~0x02;
2016-05-14 22:19:51 +02:00
// switch off SCSMR_2.TE and SCSMR_2.RE
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030;
2016-05-14 22:19:51 +02:00
// SCIF2 clock off (STBCR3.MSTP31)
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7337_STBCR3 |= 0x02;
2016-05-14 22:19:51 +02:00
// set bit 6 of port G to output mode
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000;
2016-05-14 22:19:51 +02:00
// set bit 5 and 6 of port G
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7337_PGDR |= 0x60;
2016-05-14 22:19:51 +02:00
// set port SC bit 0 to output
2016-05-16 13:32:58 +02:00
*(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001;
2016-05-14 22:19:51 +02:00
}
/*
// set port J bit 2 to output
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010;
// set port J bit 3 to input
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080;*/
}
void SetPin()
{
if(is_SH4)
{
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7305_PJDR |= 0x04;
*(volatile unsigned char*)SH7305_PJDR &= ~0x08;
2016-05-14 22:19:51 +02:00
//set pin to 0x4B
}
else
{
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7337_SCPDR |= 0x01;
2016-05-14 22:19:51 +02:00
}
}
void ResetPin()
{
if(is_SH4)
{
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7305_PJDR &= ~0x04;
*(volatile unsigned char*)SH7305_PJDR |= 0x08;
2016-05-14 22:19:51 +02:00
// set the pin to 0x47
}
else
{
2016-05-16 13:32:58 +02:00
*(volatile unsigned char*)SH7337_SCPDR &= ~0x01;
2016-05-14 22:19:51 +02:00
}
}
char getMPU(void)
{
// Port L control register.
volatile unsigned short *plcr = (unsigned short *)0xa4000114;
// Saved value for PLCR.
unsigned short saved_plcr;
unsigned int tested_plcr;
saved_plcr = *plcr;
*plcr = 0xffff;
tested_plcr = *plcr;
*plcr = saved_plcr;
if(tested_plcr == 0x00ff || tested_plcr == 0x0fff)
{
return 0; // MPU_SH3
}
return 1; // MPU_SH4
}