-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibspeech.c
135 lines (110 loc) · 3.32 KB
/
libspeech.c
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
#include <peekpoke.h>
#include <stdbool.h>
#define SPCARD_SLOT 2
#define SPCARD_ADDRL 0xC080+SPCARD_SLOT*0x10 + 0
#define SPCARD_ADDRH 0xC080+SPCARD_SLOT*0x10 + 1
#define SPCARD_DATA 0xC080+SPCARD_SLOT*0x10 + 2
#define SPCARD_CTRL 0xC080+SPCARD_SLOT*0x10 + 3
#define SPCARD_STATE 0xC080+SPCARD_SLOT*0x10 + 4
#define CTRL_28 1
#define CTRL_4 2
#define CTRL_PDC 4
#define STATE_BUSY 128 // 0b10000000
#define SPRAM_SIZE 4096
void load_to_spram(unsigned char* data,unsigned int data_len, unsigned int data_offset, unsigned int spram_address) {
//__asm__("nop");
unsigned char data_byte=0;
unsigned int i=0;
for (i=0; i<data_len; ++i) {
data_byte=data[i+data_offset];
//printf("Data #%d - %x ",i,data_byte);
// write byte to spram
//printf("H:%X L:%X\n",(spram_address+i) >> 8,(spram_address+i) & 0xff);
POKE(SPCARD_ADDRH,(spram_address+i) >> 8);
POKE(SPCARD_ADDRL,(spram_address+i) & 0xff);
POKE(SPCARD_DATA,data_byte);
//if (i>10) return;
}
}
unsigned char get_byte_from_spram(unsigned int spram_address) {
// read byte from spram
//printf("H:%X L:%X\n",(spram_address+i) >> 8,(spram_address+i) & 0xff);
POKE(SPCARD_ADDRH,(spram_address) >> 8);
POKE(SPCARD_ADDRL,(spram_address) & 0xff);
return PEEK(SPCARD_DATA);
}
void set_spram_addr(unsigned int spram_address) {
POKE(SPCARD_ADDRH,spram_address >> 8);
POKE(SPCARD_ADDRL,spram_address & 0xff);
}
void spcard_reset (void) {
char i,j;
// triple reset
for (j=0; j<3; ++j) {
// reset
POKE(SPCARD_CTRL,0);
for (i=0; i<3; ++i) {
POKE(SPCARD_CTRL,CTRL_PDC);
POKE(SPCARD_CTRL,0);
}
POKE(SPCARD_CTRL,0);
}
// test busy
POKE(SPCARD_CTRL, (CTRL_4 | CTRL_28));
for (i=0; i<3; ++i) {
POKE(SPCARD_CTRL,(CTRL_PDC | CTRL_4 | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
}
POKE(SPCARD_CTRL, (CTRL_4 | CTRL_28));
}
void spcard_talk () {
// talk
POKE(SPCARD_CTRL,(CTRL_28));
POKE(SPCARD_CTRL,(CTRL_PDC | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_28));
// test talk
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_PDC | CTRL_4 | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_PDC | CTRL_4 | CTRL_28));
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
}
bool spcard_check_busy () {
if ((PEEK(SPCARD_STATE) & STATE_BUSY) == 0) {
// inhibit busy
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
__asm__("nop");__asm__("nop");
POKE(SPCARD_CTRL,(CTRL_PDC | CTRL_4 | CTRL_28));
__asm__("nop");__asm__("nop");
POKE(SPCARD_CTRL,(CTRL_4 | CTRL_28));
__asm__("nop");__asm__("nop");
return false;
}
return true;
}
bool spcard_wait_talk_end () {
unsigned i=0;
unsigned char x,y;
while (spcard_check_busy()) {
++i;
if (i==1<<16-1) {
spcard_reset();
x=wherex();y=wherey();
cputsxy(0,23,"BUSY!!");
gotoxy(x,y);
return false;
}
}
return true;
}
void spcard_say(unsigned int start_addr, unsigned char wait_for_ending) {
set_spram_addr(start_addr);
spcard_talk ();
if (wait_for_ending==1) {
spcard_wait_talk_end ();
} else return;
}
void spcard_ram_say(unsigned char *data, unsigned int data_pos, unsigned int data_size, unsigned int spram_addr, char wait_for_ending) {
load_to_spram((unsigned char*)data,data_size,data_pos,spram_addr);
spcard_say(spram_addr,wait_for_ending);
}