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