openMSX
VLM5030.cc
Go to the documentation of this file.
1/*
2 vlm5030.c
3
4 VLM5030 emulator
5
6 Written by Tatsuyuki Satoh
7 Based on TMS5220 simulator (tms5220.c)
8
9 note:
10 memory read cycle(==sampling rate) = 122.9u(440clock)
11 interpolator (LC8109 = 2.5ms) = 20 * samples(125us)
12 frame time (20ms) = 4 * interpolator
13 9bit DAC is composed of 5bit Physical and 3bitPWM.
14
15 todo:
16 Noise Generator circuit without 'rand()' function.
17
18----------- command format (Analytical result) ----------
19
201)end of speech (8bit)
21:00000011:
22
232)silent some frame (8bit)
24:????SS01:
25
26SS : number of silent frames
27 00 = 2 frame
28 01 = 4 frame
29 10 = 6 frame
30 11 = 8 frame
31
323)-speech frame (48bit)
33function: 6th : 5th : 4th : 3rd : 2nd : 1st :
34end : --- : --- : --- : --- : --- :00000011:
35silent : --- : --- : --- : --- : --- :0000SS01:
36speech :11111122:22233334:44455566:67778889:99AAAEEE:EEPPPPP0:
37
38EEEEE : energy : volume 0=off,0x1f=max
39PPPPP : pitch : 0=noise , 1=fast,0x1f=slow
40111111 : K1 : 48=off
4122222 : K2 : 0=off,1=+min,0x0f=+max,0x10=off,0x11=+max,0x1f=-min
42 : 16 == special function??
433333 : K3 : 0=off,1=+min,0x07=+max,0x08=-max,0x0f=-min
444444 : K4 :
45555 : K5 : 0=off,1=+min,0x03=+max,0x04=-max,0x07=-min
46666 : K6 :
47777 : K7 :
48888 : K8 :
49999 : K9 :
50AAA : K10 :
51
52 ---------- chirp table information ----------
53
54DAC PWM cycle == 88system clock , (11clock x 8 pattern) = 40.6KHz
55one chirp == 5 x PWM cycle == 440system clock(8,136Hz)
56
57chirp 0 : volume 10- 8 : with filter
58chirp 1 : volume 8- 6 : with filter
59chirp 2 : volume 6- 4 : with filter
60chirp 3 : volume 4 : no filter ??
61chirp 4- 5: volume 4- 2 : with filter
62chirp 6-11: volume 2- 0 : with filter
63chirp 12-..: volume 0 : silent
64
65 ---------- digital output information ----------
66 when ME pin = high , some status output to A0..15 pins
67
68 A0..8 : DAC output value (abs)
69 A9 : DAC sign flag , L=minus,H=Plus
70 A10 : energy reload flag (pitch pulse)
71 A11..15 : unknown
72
73 [DAC output value(signed 6bit)] = A9 ? A0..8 : -(A0..8)
74
75*/
76
77#include "VLM5030.hh"
78#include "DeviceConfig.hh"
79#include "XMLElement.hh"
80#include "FileOperations.hh"
81#include "cstd.hh"
82#include "narrow.hh"
83#include "one_of.hh"
84#include "random.hh"
85#include "ranges.hh"
86#include "serialize.hh"
87#include "xrange.hh"
88#include <algorithm>
89#include <array>
90#include <cmath>
91
92namespace openmsx {
93
94
95// interpolator per frame
96static constexpr int FR_SIZE = 4;
97// samples per interpolator
98static constexpr int IP_SIZE_SLOWER = 240 / FR_SIZE;
99static constexpr int IP_SIZE_SLOW = 200 / FR_SIZE;
100static constexpr int IP_SIZE_NORMAL = 160 / FR_SIZE;
101static constexpr int IP_SIZE_FAST = 120 / FR_SIZE;
102static constexpr int IP_SIZE_FASTER = 80 / FR_SIZE;
103
104// phase value
105enum {
112 PH_END
114
115// speed parameter
116// SPC SPB SPA
117// 1 0 1 more slow (05h) : 42ms (150%) : 60sample
118// 1 1 x slow (06h,07h) : 34ms (125%) : 50sample
119// x 0 0 normal (00h,04h) : 25.6ms (100%) : 40sample
120// 0 0 1 fast (01h) : 20.2ms (75%) : 30sample
121// 0 1 x more fast (02h,03h) : 12.2ms (50%) : 20sample
122static constexpr std::array<int, 8> VLM5030_speed_table =
123{
124 IP_SIZE_NORMAL,
125 IP_SIZE_FAST,
126 IP_SIZE_FASTER,
127 IP_SIZE_FASTER,
128 IP_SIZE_NORMAL,
129 IP_SIZE_SLOWER,
130 IP_SIZE_SLOW,
131 IP_SIZE_SLOW
132};
133
134// ROM Tables
135
136// This is the energy lookup table
137
138// sampled from real chip
139static constexpr std::array<uint16_t, 0x20> energyTable =
140{
141 0, 2, 4, 6, 10, 12, 14, 18, // 0-7
142 22, 26, 30, 34, 38, 44, 48, 54, // 8-15
143 62, 68, 76, 84, 94,102,114,124, // 16-23
144 136,150,164,178,196,214,232,254 // 24-31
145};
146
147// This is the pitch lookup table
148static constexpr std::array<uint8_t, 0x20> pitchTable =
149{
150 1, // 0 : random mode
151 22, // 1 : start=22
152 23, 24, 25, 26, 27, 28, 29, 30, // 2- 9 : 1step
153 32, 34, 36, 38, 40, 42, 44, 46, // 10-17 : 2step
154 50, 54, 58, 62, 66, 70, 74, 78, // 18-25 : 4step
155 86, 94, 102,110,118,126 // 26-31 : 8step
156};
157
158static constexpr std::array<int16_t, 64> K1_table = {
159 -24898, -25672, -26446, -27091, -27736, -28252, -28768, -29155,
160 -29542, -29929, -30316, -30574, -30832, -30961, -31219, -31348,
161 -31606, -31735, -31864, -31864, -31993, -32122, -32122, -32251,
162 -32251, -32380, -32380, -32380, -32509, -32509, -32509, -32509,
163 24898, 23995, 22963, 21931, 20770, 19480, 18061, 16642,
164 15093, 13416, 11610, 9804, 7998, 6063, 3999, 1935,
165 0, -1935, -3999, -6063, -7998, -9804, -11610, -13416,
166 -15093, -16642, -18061, -19480, -20770, -21931, -22963, -23995
167};
168static constexpr std::array<int16_t, 32> K2_table = {
169 0, -3096, -6321, -9417, -12513, -15351, -18061, -20770,
170 -23092, -25285, -27220, -28897, -30187, -31348, -32122, -32638,
171 0, 32638, 32122, 31348, 30187, 28897, 27220, 25285,
172 23092, 20770, 18061, 15351, 12513, 9417, 6321, 3096
173};
174static constexpr std::array<int16_t, 16> K3_table = {
175 0, -3999, -8127, -12255, -16384, -20383, -24511, -28639,
176 32638, 28639, 24511, 20383, 16254, 12255, 8127, 3999
177};
178static constexpr std::array<int16_t, 8> K5_table = {
179 0, -8127, -16384, -24511, 32638, 24511, 16254, 8127
180};
181
182unsigned VLM5030::getBits(unsigned sBit, unsigned bits)
183{
184 unsigned offset = address + (sBit / 8);
185 unsigned data = rom[(offset + 0) & address_mask] +
186 rom[(offset + 1) & address_mask] * 256;
187 data >>= (sBit & 7);
188 data &= (0xFF >> (8 - bits));
189 return data;
190}
191
192// get next frame
193int VLM5030::parseFrame()
194{
195 // remember previous frame
196 old_energy = new_energy;
197 old_pitch = new_pitch;
198 old_k = new_k;
199 // command byte check
200 uint8_t cmd = rom[address & address_mask];
201 if (cmd & 0x01) {
202 // extend frame
203 new_energy = new_pitch = 0;
204 ranges::fill(new_k, 0);
205 ++address;
206 if (cmd & 0x02) {
207 // end of speech
208 return 0;
209 } else {
210 // silent frame
211 int nums = ((cmd >> 2) + 1) * 2;
212 return nums * FR_SIZE;
213 }
214 }
215 // pitch
216 new_pitch = (pitchTable[getBits(1, 5)] + pitch_offset) & 0xff;
217 // energy
218 new_energy = energyTable[getBits(6, 5)];
219
220 // 10 K's
221 new_k[9] = K5_table[getBits(11, 3)];
222 new_k[8] = K5_table[getBits(14, 3)];
223 new_k[7] = K5_table[getBits(17, 3)];
224 new_k[6] = K5_table[getBits(20, 3)];
225 new_k[5] = K5_table[getBits(23, 3)];
226 new_k[4] = K5_table[getBits(26, 3)];
227 new_k[3] = K3_table[getBits(29, 4)];
228 new_k[2] = K3_table[getBits(33, 4)];
229 new_k[1] = K2_table[getBits(37, 5)];
230 new_k[0] = K1_table[getBits(42, 6)];
231
232 address += 6;
233 return FR_SIZE;
234}
235
236// decode and buffering data
237void VLM5030::generateChannels(std::span<float*> bufs, unsigned num)
238{
239 // Single channel device: replace content of bufs[0] (not add to it).
240 if (phase == PH_IDLE) {
241 bufs[0] = nullptr;
242 return;
243 }
244
245 int buf_count = 0;
246
247 // running
248 if (phase == one_of(PH_RUN, PH_STOP)) {
249 // playing speech
250 while (num > 0) {
251 // check new interpolator or new frame
252 if (sample_count == 0) {
253 if (phase == PH_STOP) {
254 phase = PH_END;
255 sample_count = 1;
256 goto phase_stop; // continue to end phase
257 }
258 sample_count = frame_size;
259 // interpolator changes
260 if (interp_count == 0) {
261 // change to new frame
262 interp_count = parseFrame(); // with change phase
263 if (interp_count == 0) {
264 // end mark found
265 interp_count = FR_SIZE;
266 sample_count = frame_size; // end -> stop time
267 phase = PH_STOP;
268 }
269 // Set old target as new start of frame
270 current_energy = old_energy;
271 current_pitch = old_pitch;
272 ranges::copy(old_k, current_k); // no assignment because arrays have different type (intentional?)
273 // is this a zero energy frame?
274 if (current_energy == 0) {
275 target_energy = 0;
276 target_pitch = current_pitch;
277 ranges::copy(current_k, target_k); // no assignment because arrays have different type (intentional?)
278 } else {
279 // normal frame
280 target_energy = new_energy;
281 target_pitch = new_pitch;
282 target_k = new_k;
283 }
284 }
285 // next interpolator
286 // Update values based on step values 25%, 50%, 75%, 100%
287 interp_count -= interp_step;
288 // 3,2,1,0 -> 1,2,3,4
289 int interp_effect = FR_SIZE - (interp_count % FR_SIZE);
290 current_energy = old_energy + (target_energy - old_energy) * interp_effect / FR_SIZE;
291 if (old_pitch > 1) {
292 current_pitch = old_pitch + (target_pitch - old_pitch) * interp_effect / FR_SIZE;
293 }
294 for (auto i : xrange(10))
295 current_k[i] = old_k[i] + (target_k[i] - old_k[i]) * interp_effect / FR_SIZE;
296 }
297 // calculate digital filter
298 int current_val = [&] {
299 if (old_energy == 0) {
300 // generate silent samples here
301 return 0;
302 } else if (old_pitch <= 1) {
303 // generate unvoiced samples here
304 return random_bool() ? int(current_energy)
305 : -int(current_energy);
306 } else {
307 // generate voiced samples here
308 return (pitch_count == 0) ? int(current_energy) : 0;
309 }
310 }();
311
312 // Lattice filter here
313 std::array<int, 11> u;
314 u[10] = current_val;
315 for (int i = 9; i >= 0; --i) {
316 u[i] = u[i + 1] - ((current_k[i] * x[i]) / 32768);
317 }
318 for (int i = 9; i >= 1; --i) {
319 x[i] = x[i - 1] + ((current_k[i - 1] * u[i - 1]) / 32768);
320 }
321 x[0] = u[0];
322
323 // clipping, buffering
324 bufs[0][buf_count] = narrow<float>(std::clamp(u[0], -511, 511));
325 ++buf_count;
326 --sample_count;
327 ++pitch_count;
328 if (pitch_count >= current_pitch) {
329 pitch_count = 0;
330 }
331 --num;
332 }
333 // return;
334 }
335phase_stop:
336 switch (phase) {
337 case PH_SETUP:
338 if (sample_count <= num) {
339 sample_count = 0;
340 // pin_BSY = true;
341 phase = PH_WAIT;
342 } else {
343 sample_count -= num;
344 }
345 break;
346 case PH_END:
347 if (sample_count <= num) {
348 sample_count = 0;
349 pin_BSY = false;
350 phase = PH_IDLE;
351 } else {
352 sample_count -= num;
353 }
354 }
355 // silent buffering
356 while (num > 0) {
357 bufs[0][buf_count++] = 0;
358 --num;
359 }
360}
361
362float VLM5030::getAmplificationFactorImpl() const
363{
364 return 1.0f / (1 << 9);
365}
366
367// setup parameter option when RST=H
368void VLM5030::setupParameter(uint8_t param)
369{
370 // latch parameter value
371 parameter = param;
372
373 // bit 0,1 : 4800bps / 9600bps , interpolator step
374 if (param & 2) { // bit 1 = 1 , 9600bps
375 interp_step = 4; // 9600bps : no interpolator
376 } else if (param & 1) { // bit1 = 0 & bit0 = 1 , 4800bps
377 interp_step = 2; // 4800bps : 2 interpolator
378 } else { // bit1 = bit0 = 0 : 2400bps
379 interp_step = 1; // 2400bps : 4 interpolator
380 }
381
382 // bit 3,4,5 : speed (frame size)
383 frame_size = VLM5030_speed_table[(param >> 3) & 7];
384
385 // bit 6,7 : low / high pitch
386 if (param & 0x80) { // bit7=1 , high pitch
387 pitch_offset = -8;
388 } else if (param & 0x40) { // bit6=1 , low pitch
389 pitch_offset = 8;
390 } else {
391 pitch_offset = 0;
392 }
393}
394
396{
397 phase = PH_RESET;
398 address = 0;
399 vcu_addr_h = 0;
400 pin_BSY = false;
401
402 old_energy = old_pitch = 0;
403 new_energy = new_pitch = 0;
404 current_energy = current_pitch = 0;
405 target_energy = target_pitch = 0;
406 ranges::fill(old_k, 0);
407 ranges::fill(new_k, 0);
408 ranges::fill(current_k, 0);
409 ranges::fill(target_k, 0);
410 interp_count = sample_count = pitch_count = 0;
411 ranges::fill(x, 0);
412 // reset parameters
413 setupParameter(0x00);
414}
415
416// get BSY pin level
417bool VLM5030::getBSY(EmuTime::param time) const
418{
419 const_cast<VLM5030*>(this)->updateStream(time);
420 return pin_BSY;
421}
422
423// latch control data
424void VLM5030::writeData(uint8_t data)
425{
426 latch_data = data;
427}
428
429void VLM5030::writeControl(uint8_t data, EmuTime::param time)
430{
431 updateStream(time);
432 setRST((data & 0x01) != 0);
433 setVCU((data & 0x04) != 0);
434 setST ((data & 0x02) != 0);
435}
436
437// set RST pin level : reset / set table address A8-A15
438void VLM5030::setRST(bool pin)
439{
440 if (pin_RST) {
441 if (!pin) { // H -> L : latch parameters
442 pin_RST = false;
443 setupParameter(latch_data);
444 }
445 } else {
446 if (pin) { // L -> H : reset chip
447 pin_RST = true;
448 if (pin_BSY) {
449 reset();
450 }
451 }
452 }
453}
454
455// set VCU pin level : ?? unknown
456void VLM5030::setVCU(bool pin)
457{
458 // direct mode / indirect mode
459 pin_VCU = pin;
460}
461
462// set ST pin level : set table address A0-A7 / start speech
463void VLM5030::setST(bool pin)
464{
465 if (pin_ST == pin) {
466 // pin level unchanged
467 return;
468 }
469 if (!pin) {
470 // H -> L
471 pin_ST = false;
472 if (pin_VCU) {
473 // direct access mode & address High
474 vcu_addr_h = (int(latch_data) << 8) + 0x01;
475 } else {
476 // check access mode
477 if (vcu_addr_h) {
478 // direct access mode
479 address = (vcu_addr_h & 0xff00) + latch_data;
480 vcu_addr_h = 0;
481 } else {
482 // indirect access mode
483 int table = (latch_data & 0xfe) + ((int(latch_data) & 1) << 8);
484 address = ((rom[(table + 0) & address_mask]) << 8) |
485 rom[(table + 1) & address_mask];
486 }
487 // reset process status
488 sample_count = frame_size;
489 interp_count = FR_SIZE;
490 // clear filter
491 // start after 3 sampling cycle
492 phase = PH_RUN;
493 }
494 } else {
495 // L -> H
496 pin_ST = true;
497 // setup speech, BSY on after 30ms?
498 phase = PH_SETUP;
499 sample_count = 1; // wait time for busy on
500 pin_BSY = true;
501 }
502}
503
504
505static XMLElement* getRomConfig(
506 DeviceConfig& config, const std::string& name, std::string_view romFilename)
507{
508 auto& doc = config.getXMLDocument();
509 auto* voiceROMconfig = doc.allocateElement(doc.allocateString(name));
510 voiceROMconfig->setFirstAttribute(doc.allocateAttribute("id", "name"));
511 auto* romElement = voiceROMconfig->setFirstChild(doc.allocateElement("rom"));
512 romElement->setFirstChild(doc.allocateElement( // load by sha1sum
513 "sha1", "4f36d139ee4baa7d5980f765de9895570ee05f40"))
514 ->setNextSibling(doc.allocateElement( // load by predefined filename in software rom's dir
515 "filename",
516 doc.allocateString(tmpStrCat(FileOperations::stripExtension(romFilename), "_voice.rom"))))
517 ->setNextSibling(doc.allocateElement( // or hardcoded filename in ditto dir
518 "filename", "keyboardmaster/voice.rom"));
519 return voiceROMconfig;
520}
521
522static constexpr auto INPUT_RATE = unsigned(cstd::round(3579545 / 440.0));
523
524VLM5030::VLM5030(const std::string& name_, static_string_view desc,
525 std::string_view romFilename, const DeviceConfig& config)
526 : ResampledSoundDevice(config.getMotherBoard(), name_, desc, 1, INPUT_RATE, false)
527 , rom(name_ + " ROM", "rom", DeviceConfig(config, *getRomConfig(const_cast<DeviceConfig&>(config), name_, romFilename)))
528{
529 reset();
530 phase = PH_IDLE;
531
532 assert(rom.size() != 0);
533 address_mask = narrow<unsigned>(rom.size() - 1);
534
535 registerSound(config);
536}
537
539{
541}
542
543template<typename Archive>
544void VLM5030::serialize(Archive& ar, unsigned /*version*/)
545{
546 ar.serialize("address_mask", address_mask,
547 "frame_size", frame_size,
548 "pitch_offset", pitch_offset,
549 "current_energy", current_energy,
550 "current_pitch", current_pitch,
551 "current_k", current_k,
552 "x", x,
553 "address", address,
554 "vcu_addr_h", vcu_addr_h,
555 "old_k", old_k,
556 "new_k", new_k,
557 "target_k", target_k,
558 "old_energy", old_energy,
559 "new_energy", new_energy,
560 "target_energy", target_energy,
561 "old_pitch", old_pitch,
562 "new_pitch", new_pitch,
563 "target_pitch", target_pitch,
564 "interp_step", interp_step,
565 "interp_count", interp_count,
566 "sample_count", sample_count,
567 "pitch_count", pitch_count,
568 "latch_data", latch_data,
569 "parameter", parameter,
570 "phase", phase,
571 "pin_BSY", pin_BSY,
572 "pin_ST", pin_ST,
573 "pin_VCU", pin_VCU,
574 "pin_RST", pin_RST);
575}
576
578
579} // namespace openmsx
Definition: one_of.hh:7
auto size() const
Definition: Rom.hh:36
void updateStream(EmuTime::param time)
Definition: SoundDevice.cc:138
void unregisterSound()
Unregisters this sound device with the Mixer.
Definition: SoundDevice.cc:133
void registerSound(const DeviceConfig &config)
Registers this sound device with the Mixer.
Definition: SoundDevice.cc:88
void writeData(uint8_t data)
latch control data
Definition: VLM5030.cc:424
bool getBSY(EmuTime::param time) const
get BSY pin level
Definition: VLM5030.cc:417
VLM5030(const std::string &name, static_string_view desc, std::string_view romFilename, const DeviceConfig &config)
Definition: VLM5030.cc:524
void writeControl(uint8_t data, EmuTime::param time)
set RST / VCU / ST pins
Definition: VLM5030.cc:429
void serialize(Archive &ar, unsigned version)
Definition: VLM5030.cc:544
XMLAttribute * setFirstAttribute(XMLAttribute *attribute)
Definition: XMLElement.hh:221
static_string_view
constexpr double round(double x)
Definition: cstd.hh:247
constexpr vecN< N, T > clamp(const vecN< N, T > &x, const vecN< N, T > &minVal, const vecN< N, T > &maxVal)
Definition: gl_vec.hh:293
string_view stripExtension(string_view path)
Returns the path without extension.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
@ PH_RUN
Definition: VLM5030.cc:110
@ PH_END
Definition: VLM5030.cc:112
@ PH_RESET
Definition: VLM5030.cc:106
@ PH_IDLE
Definition: VLM5030.cc:107
@ PH_SETUP
Definition: VLM5030.cc:108
@ PH_STOP
Definition: VLM5030.cc:111
@ PH_WAIT
Definition: VLM5030.cc:109
constexpr void fill(ForwardRange &&range, const T &value)
Definition: ranges.hh:287
auto copy(InputRange &&range, OutputIter out)
Definition: ranges.hh:232
bool random_bool()
Return a random boolean value.
Definition: random.hh:24
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1021
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:610
constexpr auto xrange(T e)
Definition: xrange.hh:133