1 /*
2  * t6721.h - Toshiba 6721a Emulation
3  *
4  * Written by
5  *  groepaz <groepaz@gmx.net>
6  *
7  * This file is part of VICE, the Versatile Commodore Emulator.
8  * See README for copyright notice.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  *  02111-1307  USA.
24  *
25  */
26 
27 #ifndef VICE_T6721_H
28 #define VICE_T6721_H
29 
30 #include "types.h"
31 
32 /* t6721a.pdf:581 status output */
33 #define T6721_STATUS_END_OF_SPEECH      0x01
34 #define T6721_STATUS_SYSTEM_POWER_DOWN  0x02
35 #define T6721_STATUS_ROM_DATA_ERROR     0x04
36 #define T6721_STATUS_COMMAND_ERROR      0x08
37 
38 /* t6721a.pdf:577,578 commands */
39 #define T6721_COMMAND_NOP               0x00
40 #define T6721_COMMAND_STRT              0x01
41 #define T6721_COMMAND_STOP              0x02
42 #define T6721_COMMAND_ADLD              0x03    /* 6 nibbles */
43 #define T6721_COMMAND_AAGN              0x04
44 #define T6721_COMMAND_SPLD              0x05    /* 2 nibbles */
45 #define T6721_COMMAND_CNDT1             0x06    /* 2 nibbles */
46 #define T6721_COMMAND_CNDT2             0x07    /* 2 nibbles */
47 #define T6721_COMMAND_RRDM              0x08
48 #define T6721_COMMAND_SPDN              0x09
49 #define T6721_COMMAND_APDN              0x0a
50 #define T6721_COMMAND_SAGN              0x0b
51 /* 0x0c..0x0f are "redundant" */
52 #define T6721_COMMAND_UND_0C            0x0c
53 #define T6721_COMMAND_UND_0D            0x0d
54 #define T6721_COMMAND_UND_0E            0x0e
55 #define T6721_COMMAND_UND_0F            0x0f
56 
57 /* t6721a.pdf:571 codes for speed */
58 #define T6721_SPEED_0_7        0x01
59 #define T6721_SPEED_0_8        0x02
60 #define T6721_SPEED_0_9        0x03
61 #define T6721_SPEED_1_0        0x04
62 #define T6721_SPEED_1_1        0x05
63 #define T6721_SPEED_1_2        0x06
64 #define T6721_SPEED_1_3        0x07
65 #define T6721_SPEED_1_4        0x08
66 #define T6721_SPEED_1_5        0x09
67 #define T6721_SPEED_1_55       0x0a
68 /* 0x00, 0x0b..0x0f are undefined */
69 #define T6721_SPEED_UND_00     0x00
70 #define T6721_SPEED_UND_0B     0x0b
71 #define T6721_SPEED_UND_0C     0x0c
72 #define T6721_SPEED_UND_0D     0x0d
73 #define T6721_SPEED_UND_0E     0x0e
74 #define T6721_SPEED_UND_0F     0x0f
75 
76 /* t6721a.pdf:580 bits of CONDITION 1 */
77 #define T6721_COND1_LOSS       (1 << 2)
78 #define T6721_COND1_SHAPE      (1 << 3)
79 /* bit 0..1 should be 0 */
80 #define T6721_COND1_UND0       (1 << 0)
81 #define T6721_COND1_UND1       (1 << 1)
82 
83 /* t6721a.pdf:580 bits of CONDITION 2 */
84 #define T6721_COND2_STAGES     (1 << 0) /* filter stages 0: 10 1: 8 */
85 #define T6721_COND2_REPEAT     (1 << 1) /* repeat 0: available 1: none */
86 #define T6721_COND2_LENGTH     (1 << 2) /* frame lemgth 0: 20ms 1: 10ms */
87 #define T6721_COND2_BPFRAME    (1 << 3) /* bits per frame 0: 48 1: 96 */
88 
89 /* parameter values for CONDITION 1 */
90 #define T6721_LOSS_DISABLED    0
91 #define T6721_LOSS_ENABLED     1
92 
93 #define T6721_SHAPE_PITCH      0
94 #define T6721_SHAPE_TRIANGLE   1
95 
96 /* parameter values for CONDITION 2 */
97 #define T6721_STAGES_8         8
98 #define T6721_STAGES_10       10
99 
100 #define T6721_REPEAT_DISABLED  0
101 #define T6721_REPEAT_ENABLED   1
102 
103 #define T6721_FRAME_10MS       1
104 #define T6721_FRAME_20MS       2
105 
106 #define T6721_FRAME_48BIT      0
107 #define T6721_FRAME_96BIT      1
108 
109 /* reading from D0..D3 either gives status or speach ROM */
110 #define T6721_READMODE_STATUS  0
111 #define T6721_READMODE_ROM     1
112 
113 /* t6721a.pdf:595 pin description */
114 
115 #define T6721_FRAMETYPE_EOS       0 /* 1 */
116 #define T6721_FRAMETYPE_ZERO      1 /* 1 */
117 #define T6721_FRAMETYPE_SILENT    2 /* 1 */
118 #define T6721_FRAMETYPE_UNVOICED  3 /* 6 */
119 #define T6721_FRAMETYPE_VOICED    4 /* 12 */
120 
121 typedef struct _t6721_state {
122     int cmd_nibbles; /* nibbles left for current command */
123     int cmd_current; /* current command */
124 
125     int speed;       /* set by speed load cmd */
126     int condition1;  /* set by condition1 cmd */
127     int condition2;  /* set by condition2 cmd */
128     /* individual parameter values set by condition1 and condition 2 */
129     int cond1_loss;
130     int cond1_shape;
131     int cond2_stages;
132     int cond2_repeat;
133     int cond2_framelen;
134     int cond2_framebits;
135 
136     int rd, wr; /* input: read and write */
137     int rd_last, wr_last; /* internal flags for edge triggered operation */
138 
139     int busy;   /* output: BSY busy */
140     int apd;    /* output: APD external APD audio power down */
141     int eos;    /* output: EOS end of speech */
142     int dtrd;   /* output: data read */
143 
144     int status; /* output: D0..D3 */
145 
146     int playing;  /* chip is playing audio */
147     int playing_delay;
148 
149     int readmode; /* chip readmode, rom or status */
150 
151     int eos_samples; /* samples left until eos is deasserted */
152 
153     int samples_per_sec; /* output samples per second */
154     int cycles_per_sec; /* CPU/System cycles per second */
155 
156     int cycles_done; /* number of cycles the chip has run since last update_output */
157 
158     uint8_t (*read_data)(struct _t6721_state*, unsigned int *bit); /* input: DI */
159     void (*set_apd)(struct _t6721_state*);
160     void (*set_eos)(struct _t6721_state*);
161     void (*set_dtrd)(struct _t6721_state*);
162 } t6721_state;
163 
164 extern void t6721_reset(t6721_state *t6721);
165 extern void t6721_sound_machine_init(t6721_state *t6721, int samples_per_sec, int cycles_per_sec);
166 extern void t6721_sound_machine_init_vbr(t6721_state *t6721, int speed, int cycles_per_sec, int factor);
167 /* read/write from/to  d0..d3 */
168 extern uint8_t t6721_read(t6721_state *t6721); /* read from d0..d3 (status) */
169 extern void t6721_store(t6721_state *t6721, uint8_t data); /* store to d0..d3 (command) */
170 /* run chip for N cpu/system cycles */
171 extern void t6721_update_tick(t6721_state *t6721);
172 extern void t6721_update_ticks(t6721_state *t6721, int ticks);
173 /* update output sound buffer, run chip (remaining ticks) */
174 extern void t6721_update_output(t6721_state *t6721, int16_t *buf, int num);
175 
176 extern int t6721_dump(t6721_state *t6721);
177 
178 struct snapshot_s;
179 extern int t6721_snapshot_read_module(struct snapshot_s *s, t6721_state *t6721);
180 extern int t6721_snapshot_write_module(struct snapshot_s *s, t6721_state *t6721);
181 
182 #endif
183