]> sigrok.org Git - libsigrok.git/blob - src/hardware/asix-sigma/protocol.c
asix-sigma: Fixup the download of the last data acquisition chunk
[libsigrok.git] / src / hardware / asix-sigma / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>,
5  * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6  * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /*
23  * ASIX SIGMA/SIGMA2 logic analyzer driver
24  */
25
26 #include <config.h>
27 #include "protocol.h"
28
29 /*
30  * The ASIX Sigma supports arbitrary integer frequency divider in
31  * the 50MHz mode. The divider is in range 1...256 , allowing for
32  * very precise sampling rate selection. This driver supports only
33  * a subset of the sampling rates.
34  */
35 SR_PRIV const uint64_t samplerates[] = {
36         SR_KHZ(200),    /* div=250 */
37         SR_KHZ(250),    /* div=200 */
38         SR_KHZ(500),    /* div=100 */
39         SR_MHZ(1),      /* div=50  */
40         SR_MHZ(5),      /* div=10  */
41         SR_MHZ(10),     /* div=5   */
42         SR_MHZ(25),     /* div=2   */
43         SR_MHZ(50),     /* div=1   */
44         SR_MHZ(100),    /* Special FW needed */
45         SR_MHZ(200),    /* Special FW needed */
46 };
47
48 SR_PRIV const size_t samplerates_count = ARRAY_SIZE(samplerates);
49
50 static const char sigma_firmware_files[][24] = {
51         /* 50 MHz, supports 8 bit fractions */
52         "asix-sigma-50.fw",
53         /* 100 MHz */
54         "asix-sigma-100.fw",
55         /* 200 MHz */
56         "asix-sigma-200.fw",
57         /* Synchronous clock from pin */
58         "asix-sigma-50sync.fw",
59         /* Frequency counter */
60         "asix-sigma-phasor.fw",
61 };
62
63 static int sigma_read(void *buf, size_t size, struct dev_context *devc)
64 {
65         int ret;
66
67         ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
68         if (ret < 0) {
69                 sr_err("ftdi_read_data failed: %s",
70                        ftdi_get_error_string(&devc->ftdic));
71         }
72
73         return ret;
74 }
75
76 static int sigma_write(void *buf, size_t size, struct dev_context *devc)
77 {
78         int ret;
79
80         ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
81         if (ret < 0) {
82                 sr_err("ftdi_write_data failed: %s",
83                        ftdi_get_error_string(&devc->ftdic));
84         } else if ((size_t) ret != size) {
85                 sr_err("ftdi_write_data did not complete write.");
86         }
87
88         return ret;
89 }
90
91 /*
92  * NOTE: We chose the buffer size to be large enough to hold any write to the
93  * device. We still print a message just in case.
94  */
95 SR_PRIV int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
96                                  struct dev_context *devc)
97 {
98         size_t i;
99         uint8_t buf[80];
100         int idx = 0;
101
102         if ((2 * len + 2) > sizeof(buf)) {
103                 sr_err("Attempted to write %zu bytes, but buffer is too small.",
104                        len);
105                 return SR_ERR_BUG;
106         }
107
108         buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
109         buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
110
111         for (i = 0; i < len; i++) {
112                 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
113                 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
114         }
115
116         return sigma_write(buf, idx, devc);
117 }
118
119 SR_PRIV int sigma_set_register(uint8_t reg, uint8_t value, struct dev_context *devc)
120 {
121         return sigma_write_register(reg, &value, 1, devc);
122 }
123
124 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
125                                struct dev_context *devc)
126 {
127         uint8_t buf[3];
128
129         buf[0] = REG_ADDR_LOW | (reg & 0xf);
130         buf[1] = REG_ADDR_HIGH | (reg >> 4);
131         buf[2] = REG_READ_ADDR;
132
133         sigma_write(buf, sizeof(buf), devc);
134
135         return sigma_read(data, len, devc);
136 }
137
138 static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
139 {
140         uint8_t value;
141
142         if (1 != sigma_read_register(reg, &value, 1, devc)) {
143                 sr_err("sigma_get_register: 1 byte expected");
144                 return 0;
145         }
146
147         return value;
148 }
149
150 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
151                           struct dev_context *devc)
152 {
153         uint8_t buf[] = {
154                 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
155
156                 REG_READ_ADDR | NEXT_REG,
157                 REG_READ_ADDR | NEXT_REG,
158                 REG_READ_ADDR | NEXT_REG,
159                 REG_READ_ADDR | NEXT_REG,
160                 REG_READ_ADDR | NEXT_REG,
161                 REG_READ_ADDR | NEXT_REG,
162         };
163         uint8_t result[6];
164
165         sigma_write(buf, sizeof(buf), devc);
166
167         sigma_read(result, sizeof(result), devc);
168
169         *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
170         *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
171
172         /* Not really sure why this must be done, but according to spec. */
173         if ((--*stoppos & 0x1ff) == 0x1ff)
174                 *stoppos -= 64;
175
176         if ((*--triggerpos & 0x1ff) == 0x1ff)
177                 *triggerpos -= 64;
178
179         return 1;
180 }
181
182 static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
183                            uint8_t *data, struct dev_context *devc)
184 {
185         size_t i;
186         uint8_t buf[4096];
187         int idx;
188
189         /* Send the startchunk. Index start with 1. */
190         idx = 0;
191         buf[idx++] = startchunk >> 8;
192         buf[idx++] = startchunk & 0xff;
193         sigma_write_register(WRITE_MEMROW, buf, idx, devc);
194
195         /* Read the DRAM. */
196         idx = 0;
197         buf[idx++] = REG_DRAM_BLOCK;
198         buf[idx++] = REG_DRAM_WAIT_ACK;
199
200         for (i = 0; i < numchunks; i++) {
201                 /* Alternate bit to copy from DRAM to cache. */
202                 if (i != (numchunks - 1))
203                         buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
204
205                 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
206
207                 if (i != (numchunks - 1))
208                         buf[idx++] = REG_DRAM_WAIT_ACK;
209         }
210
211         sigma_write(buf, idx, devc);
212
213         return sigma_read(data, numchunks * CHUNK_SIZE, devc);
214 }
215
216 /* Upload trigger look-up tables to Sigma. */
217 SR_PRIV int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *devc)
218 {
219         int i;
220         uint8_t tmp[2];
221         uint16_t bit;
222
223         /* Transpose the table and send to Sigma. */
224         for (i = 0; i < 16; i++) {
225                 bit = 1 << i;
226
227                 tmp[0] = tmp[1] = 0;
228
229                 if (lut->m2d[0] & bit)
230                         tmp[0] |= 0x01;
231                 if (lut->m2d[1] & bit)
232                         tmp[0] |= 0x02;
233                 if (lut->m2d[2] & bit)
234                         tmp[0] |= 0x04;
235                 if (lut->m2d[3] & bit)
236                         tmp[0] |= 0x08;
237
238                 if (lut->m3 & bit)
239                         tmp[0] |= 0x10;
240                 if (lut->m3s & bit)
241                         tmp[0] |= 0x20;
242                 if (lut->m4 & bit)
243                         tmp[0] |= 0x40;
244
245                 if (lut->m0d[0] & bit)
246                         tmp[1] |= 0x01;
247                 if (lut->m0d[1] & bit)
248                         tmp[1] |= 0x02;
249                 if (lut->m0d[2] & bit)
250                         tmp[1] |= 0x04;
251                 if (lut->m0d[3] & bit)
252                         tmp[1] |= 0x08;
253
254                 if (lut->m1d[0] & bit)
255                         tmp[1] |= 0x10;
256                 if (lut->m1d[1] & bit)
257                         tmp[1] |= 0x20;
258                 if (lut->m1d[2] & bit)
259                         tmp[1] |= 0x40;
260                 if (lut->m1d[3] & bit)
261                         tmp[1] |= 0x80;
262
263                 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
264                                      devc);
265                 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, devc);
266         }
267
268         /* Send the parameters */
269         sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
270                              sizeof(lut->params), devc);
271
272         return SR_OK;
273 }
274
275 SR_PRIV void sigma_clear_helper(void *priv)
276 {
277         struct dev_context *devc;
278
279         devc = priv;
280
281         ftdi_deinit(&devc->ftdic);
282 }
283
284 /*
285  * Configure the FPGA for bitbang mode.
286  * This sequence is documented in section 2. of the ASIX Sigma programming
287  * manual. This sequence is necessary to configure the FPGA in the Sigma
288  * into Bitbang mode, in which it can be programmed with the firmware.
289  */
290 static int sigma_fpga_init_bitbang(struct dev_context *devc)
291 {
292         uint8_t suicide[] = {
293                 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
294         };
295         uint8_t init_array[] = {
296                 0x01, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
297                 0x01, 0x01,
298         };
299         int i, ret, timeout = (10 * 1000);
300         uint8_t data;
301
302         /* Section 2. part 1), do the FPGA suicide. */
303         sigma_write(suicide, sizeof(suicide), devc);
304         sigma_write(suicide, sizeof(suicide), devc);
305         sigma_write(suicide, sizeof(suicide), devc);
306         sigma_write(suicide, sizeof(suicide), devc);
307
308         /* Section 2. part 2), do pulse on D1. */
309         sigma_write(init_array, sizeof(init_array), devc);
310         ftdi_usb_purge_buffers(&devc->ftdic);
311
312         /* Wait until the FPGA asserts D6/INIT_B. */
313         for (i = 0; i < timeout; i++) {
314                 ret = sigma_read(&data, 1, devc);
315                 if (ret < 0)
316                         return ret;
317                 /* Test if pin D6 got asserted. */
318                 if (data & (1 << 5))
319                         return 0;
320                 /* The D6 was not asserted yet, wait a bit. */
321                 g_usleep(10 * 1000);
322         }
323
324         return SR_ERR_TIMEOUT;
325 }
326
327 /*
328  * Configure the FPGA for logic-analyzer mode.
329  */
330 static int sigma_fpga_init_la(struct dev_context *devc)
331 {
332         /* Initialize the logic analyzer mode. */
333         uint8_t mode_regval = WMR_SDRAMINIT;
334         uint8_t logic_mode_start[] = {
335                 REG_ADDR_LOW  | (READ_ID & 0xf),
336                 REG_ADDR_HIGH | (READ_ID >> 4),
337                 REG_READ_ADDR,  /* Read ID register. */
338
339                 REG_ADDR_LOW | (WRITE_TEST & 0xf),
340                 REG_DATA_LOW | 0x5,
341                 REG_DATA_HIGH_WRITE | 0x5,
342                 REG_READ_ADDR,  /* Read scratch register. */
343
344                 REG_DATA_LOW | 0xa,
345                 REG_DATA_HIGH_WRITE | 0xa,
346                 REG_READ_ADDR,  /* Read scratch register. */
347
348                 REG_ADDR_LOW | (WRITE_MODE & 0xf),
349                 REG_DATA_LOW | (mode_regval & 0xf),
350                 REG_DATA_HIGH_WRITE | (mode_regval >> 4),
351         };
352
353         uint8_t result[3];
354         int ret;
355
356         /* Initialize the logic analyzer mode. */
357         sigma_write(logic_mode_start, sizeof(logic_mode_start), devc);
358
359         /* Expect a 3 byte reply since we issued three READ requests. */
360         ret = sigma_read(result, 3, devc);
361         if (ret != 3)
362                 goto err;
363
364         if (result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa)
365                 goto err;
366
367         return SR_OK;
368 err:
369         sr_err("Configuration failed. Invalid reply received.");
370         return SR_ERR;
371 }
372
373 /*
374  * Read the firmware from a file and transform it into a series of bitbang
375  * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
376  * by the caller of this function.
377  */
378 static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
379                               uint8_t **bb_cmd, gsize *bb_cmd_size)
380 {
381         size_t i, file_size, bb_size;
382         char *firmware;
383         uint8_t *bb_stream, *bbs;
384         uint32_t imm;
385         int bit, v;
386         int ret = SR_OK;
387
388         /* Retrieve the on-disk firmware file content. */
389         firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
390                         name, &file_size, 256 * 1024);
391         if (!firmware)
392                 return SR_ERR;
393
394         /* Unscramble the file content (XOR with "random" sequence). */
395         imm = 0x3f6df2ab;
396         for (i = 0; i < file_size; i++) {
397                 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
398                 firmware[i] ^= imm & 0xff;
399         }
400
401         /*
402          * Generate a sequence of bitbang samples. With two samples per
403          * FPGA configuration bit, providing the level for the DIN signal
404          * as well as two edges for CCLK. See Xilinx UG332 for details
405          * ("slave serial" mode).
406          *
407          * Note that CCLK is inverted in hardware. That's why the
408          * respective bit is first set and then cleared in the bitbang
409          * sample sets. So that the DIN level will be stable when the
410          * data gets sampled at the rising CCLK edge, and the signals'
411          * setup time constraint will be met.
412          *
413          * The caller will put the FPGA into download mode, will send
414          * the bitbang samples, and release the allocated memory.
415          */
416         bb_size = file_size * 8 * 2;
417         bb_stream = (uint8_t *)g_try_malloc(bb_size);
418         if (!bb_stream) {
419                 sr_err("%s: Failed to allocate bitbang stream", __func__);
420                 ret = SR_ERR_MALLOC;
421                 goto exit;
422         }
423         bbs = bb_stream;
424         for (i = 0; i < file_size; i++) {
425                 for (bit = 7; bit >= 0; bit--) {
426                         v = (firmware[i] & (1 << bit)) ? 0x40 : 0x00;
427                         *bbs++ = v | 0x01;
428                         *bbs++ = v;
429                 }
430         }
431
432         /* The transformation completed successfully, return the result. */
433         *bb_cmd = bb_stream;
434         *bb_cmd_size = bb_size;
435
436 exit:
437         g_free(firmware);
438         return ret;
439 }
440
441 static int upload_firmware(struct sr_context *ctx,
442                 int firmware_idx, struct dev_context *devc)
443 {
444         int ret;
445         unsigned char *buf;
446         unsigned char pins;
447         size_t buf_size;
448         const char *firmware;
449         struct ftdi_context *ftdic;
450
451         /* Avoid downloading the same firmware multiple times. */
452         firmware = sigma_firmware_files[firmware_idx];
453         if (devc->cur_firmware == firmware_idx) {
454                 sr_info("Not uploading firmware file '%s' again.", firmware);
455                 return SR_OK;
456         }
457
458         /* Make sure it's an ASIX SIGMA. */
459         ftdic = &devc->ftdic;
460         ret = ftdi_usb_open_desc(ftdic, USB_VENDOR, USB_PRODUCT,
461                                  USB_DESCRIPTION, NULL);
462         if (ret < 0) {
463                 sr_err("ftdi_usb_open failed: %s",
464                        ftdi_get_error_string(ftdic));
465                 return 0;
466         }
467
468         ret = ftdi_set_bitmode(ftdic, 0xdf, BITMODE_BITBANG);
469         if (ret < 0) {
470                 sr_err("ftdi_set_bitmode failed: %s",
471                        ftdi_get_error_string(ftdic));
472                 return 0;
473         }
474
475         /* Four times the speed of sigmalogan - Works well. */
476         ret = ftdi_set_baudrate(ftdic, 750 * 1000);
477         if (ret < 0) {
478                 sr_err("ftdi_set_baudrate failed: %s",
479                        ftdi_get_error_string(ftdic));
480                 return 0;
481         }
482
483         /* Initialize the FPGA for firmware upload. */
484         ret = sigma_fpga_init_bitbang(devc);
485         if (ret)
486                 return ret;
487
488         /* Prepare firmware. */
489         ret = sigma_fw_2_bitbang(ctx, firmware, &buf, &buf_size);
490         if (ret != SR_OK) {
491                 sr_err("An error occurred while reading the firmware: %s",
492                        firmware);
493                 return ret;
494         }
495
496         /* Upload firmware. */
497         sr_info("Uploading firmware file '%s'.", firmware);
498         sigma_write(buf, buf_size, devc);
499
500         g_free(buf);
501
502         ret = ftdi_set_bitmode(ftdic, 0x00, BITMODE_RESET);
503         if (ret < 0) {
504                 sr_err("ftdi_set_bitmode failed: %s",
505                        ftdi_get_error_string(ftdic));
506                 return SR_ERR;
507         }
508
509         ftdi_usb_purge_buffers(ftdic);
510
511         /* Discard garbage. */
512         while (sigma_read(&pins, 1, devc) == 1)
513                 ;
514
515         /* Initialize the FPGA for logic-analyzer mode. */
516         ret = sigma_fpga_init_la(devc);
517         if (ret != SR_OK)
518                 return ret;
519
520         devc->cur_firmware = firmware_idx;
521
522         sr_info("Firmware uploaded.");
523
524         return SR_OK;
525 }
526
527 /*
528  * Sigma doesn't support limiting the number of samples, so we have to
529  * translate the number and the samplerate to an elapsed time.
530  *
531  * In addition we need to ensure that the last data cluster has passed
532  * the hardware pipeline, and became available to the PC side. With RLE
533  * compression up to 327ms could pass before another cluster accumulates
534  * at 200kHz samplerate when input pins don't change.
535  */
536 SR_PRIV uint64_t sigma_limit_samples_to_msec(const struct dev_context *devc,
537                                              uint64_t limit_samples)
538 {
539         uint64_t limit_msec;
540         uint64_t worst_cluster_time_ms;
541
542         limit_msec = limit_samples * 1000 / devc->cur_samplerate;
543         worst_cluster_time_ms = 65536 * 1000 / devc->cur_samplerate;
544         /*
545          * One cluster time is not enough to flush pipeline when sampling
546          * grounded pins with 1 sample limit at 200kHz. Hence the 2* fix.
547          */
548         return limit_msec + 2 * worst_cluster_time_ms;
549 }
550
551 SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
552 {
553         struct dev_context *devc;
554         struct drv_context *drvc;
555         size_t i;
556         int ret;
557
558         devc = sdi->priv;
559         drvc = sdi->driver->context;
560         ret = SR_OK;
561
562         /* Reject rates that are not in the list of supported rates. */
563         for (i = 0; i < samplerates_count; i++) {
564                 if (samplerates[i] == samplerate)
565                         break;
566         }
567         if (i >= samplerates_count || samplerates[i] == 0)
568                 return SR_ERR_SAMPLERATE;
569
570         /*
571          * Depending on the samplerates of 200/100/50- MHz, specific
572          * firmware is required and higher rates might limit the set
573          * of available channels.
574          */
575         if (samplerate <= SR_MHZ(50)) {
576                 ret = upload_firmware(drvc->sr_ctx, 0, devc);
577                 devc->num_channels = 16;
578         } else if (samplerate == SR_MHZ(100)) {
579                 ret = upload_firmware(drvc->sr_ctx, 1, devc);
580                 devc->num_channels = 8;
581         } else if (samplerate == SR_MHZ(200)) {
582                 ret = upload_firmware(drvc->sr_ctx, 2, devc);
583                 devc->num_channels = 4;
584         }
585
586         /*
587          * Derive the sample period from the sample rate as well as the
588          * number of samples that the device will communicate within
589          * an "event" (memory organization internal to the device).
590          */
591         if (ret == SR_OK) {
592                 devc->cur_samplerate = samplerate;
593                 devc->samples_per_event = 16 / devc->num_channels;
594                 devc->state.state = SIGMA_IDLE;
595         }
596
597         /*
598          * Support for "limit_samples" is implemented by stopping
599          * acquisition after a corresponding period of time.
600          * Re-calculate that period of time, in case the limit is
601          * set first and the samplerate gets (re-)configured later.
602          */
603         if (ret == SR_OK && devc->limit_samples) {
604                 uint64_t msecs;
605                 msecs = sigma_limit_samples_to_msec(devc, devc->limit_samples);
606                 devc->limit_msec = msecs;
607         }
608
609         return ret;
610 }
611
612 /*
613  * In 100 and 200 MHz mode, only a single pin rising/falling can be
614  * set as trigger. In other modes, two rising/falling triggers can be set,
615  * in addition to value/mask trigger for any number of channels.
616  *
617  * The Sigma supports complex triggers using boolean expressions, but this
618  * has not been implemented yet.
619  */
620 SR_PRIV int sigma_convert_trigger(const struct sr_dev_inst *sdi)
621 {
622         struct dev_context *devc;
623         struct sr_trigger *trigger;
624         struct sr_trigger_stage *stage;
625         struct sr_trigger_match *match;
626         const GSList *l, *m;
627         int channelbit, trigger_set;
628
629         devc = sdi->priv;
630         memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
631         if (!(trigger = sr_session_trigger_get(sdi->session)))
632                 return SR_OK;
633
634         trigger_set = 0;
635         for (l = trigger->stages; l; l = l->next) {
636                 stage = l->data;
637                 for (m = stage->matches; m; m = m->next) {
638                         match = m->data;
639                         if (!match->channel->enabled)
640                                 /* Ignore disabled channels with a trigger. */
641                                 continue;
642                         channelbit = 1 << (match->channel->index);
643                         if (devc->cur_samplerate >= SR_MHZ(100)) {
644                                 /* Fast trigger support. */
645                                 if (trigger_set) {
646                                         sr_err("Only a single pin trigger is "
647                                                         "supported in 100 and 200MHz mode.");
648                                         return SR_ERR;
649                                 }
650                                 if (match->match == SR_TRIGGER_FALLING)
651                                         devc->trigger.fallingmask |= channelbit;
652                                 else if (match->match == SR_TRIGGER_RISING)
653                                         devc->trigger.risingmask |= channelbit;
654                                 else {
655                                         sr_err("Only rising/falling trigger is "
656                                                         "supported in 100 and 200MHz mode.");
657                                         return SR_ERR;
658                                 }
659
660                                 trigger_set++;
661                         } else {
662                                 /* Simple trigger support (event). */
663                                 if (match->match == SR_TRIGGER_ONE) {
664                                         devc->trigger.simplevalue |= channelbit;
665                                         devc->trigger.simplemask |= channelbit;
666                                 }
667                                 else if (match->match == SR_TRIGGER_ZERO) {
668                                         devc->trigger.simplevalue &= ~channelbit;
669                                         devc->trigger.simplemask |= channelbit;
670                                 }
671                                 else if (match->match == SR_TRIGGER_FALLING) {
672                                         devc->trigger.fallingmask |= channelbit;
673                                         trigger_set++;
674                                 }
675                                 else if (match->match == SR_TRIGGER_RISING) {
676                                         devc->trigger.risingmask |= channelbit;
677                                         trigger_set++;
678                                 }
679
680                                 /*
681                                  * Actually, Sigma supports 2 rising/falling triggers,
682                                  * but they are ORed and the current trigger syntax
683                                  * does not permit ORed triggers.
684                                  */
685                                 if (trigger_set > 1) {
686                                         sr_err("Only 1 rising/falling trigger "
687                                                    "is supported.");
688                                         return SR_ERR;
689                                 }
690                         }
691                 }
692         }
693
694         return SR_OK;
695 }
696
697
698 /* Software trigger to determine exact trigger position. */
699 static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
700                               struct sigma_trigger *t)
701 {
702         int i;
703         uint16_t sample = 0;
704
705         for (i = 0; i < 8; i++) {
706                 if (i > 0)
707                         last_sample = sample;
708                 sample = samples[2 * i] | (samples[2 * i + 1] << 8);
709
710                 /* Simple triggers. */
711                 if ((sample & t->simplemask) != t->simplevalue)
712                         continue;
713
714                 /* Rising edge. */
715                 if (((last_sample & t->risingmask) != 0) ||
716                     ((sample & t->risingmask) != t->risingmask))
717                         continue;
718
719                 /* Falling edge. */
720                 if ((last_sample & t->fallingmask) != t->fallingmask ||
721                     (sample & t->fallingmask) != 0)
722                         continue;
723
724                 break;
725         }
726
727         /* If we did not match, return original trigger pos. */
728         return i & 0x7;
729 }
730
731 /*
732  * Return the timestamp of "DRAM cluster".
733  */
734 static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster)
735 {
736         return (cluster->timestamp_hi << 8) | cluster->timestamp_lo;
737 }
738
739 /*
740  * Return one 16bit data entity of a DRAM cluster at the specified index.
741  */
742 static uint16_t sigma_dram_cluster_data(struct sigma_dram_cluster *cl, int idx)
743 {
744         uint16_t sample;
745
746         sample = 0;
747         sample |= cl->samples[idx].sample_lo << 0;
748         sample |= cl->samples[idx].sample_hi << 8;
749         sample = (sample >> 8) | (sample << 8);
750         return sample;
751 }
752
753 /*
754  * Deinterlace sample data that was retrieved at 100MHz samplerate.
755  * One 16bit item contains two samples of 8bits each. The bits of
756  * multiple samples are interleaved.
757  */
758 static uint16_t sigma_deinterlace_100mhz_data(uint16_t indata, int idx)
759 {
760         uint16_t outdata;
761
762         indata >>= idx;
763         outdata = 0;
764         outdata |= (indata >> (0 * 2 - 0)) & (1 << 0);
765         outdata |= (indata >> (1 * 2 - 1)) & (1 << 1);
766         outdata |= (indata >> (2 * 2 - 2)) & (1 << 2);
767         outdata |= (indata >> (3 * 2 - 3)) & (1 << 3);
768         outdata |= (indata >> (4 * 2 - 4)) & (1 << 4);
769         outdata |= (indata >> (5 * 2 - 5)) & (1 << 5);
770         outdata |= (indata >> (6 * 2 - 6)) & (1 << 6);
771         outdata |= (indata >> (7 * 2 - 7)) & (1 << 7);
772         return outdata;
773 }
774
775 /*
776  * Deinterlace sample data that was retrieved at 200MHz samplerate.
777  * One 16bit item contains four samples of 4bits each. The bits of
778  * multiple samples are interleaved.
779  */
780 static uint16_t sigma_deinterlace_200mhz_data(uint16_t indata, int idx)
781 {
782         uint16_t outdata;
783
784         indata >>= idx;
785         outdata = 0;
786         outdata |= (indata >> (0 * 4 - 0)) & (1 << 0);
787         outdata |= (indata >> (1 * 4 - 1)) & (1 << 1);
788         outdata |= (indata >> (2 * 4 - 2)) & (1 << 2);
789         outdata |= (indata >> (3 * 4 - 3)) & (1 << 3);
790         return outdata;
791 }
792
793 static void store_sr_sample(uint8_t *samples, int idx, uint16_t data)
794 {
795         samples[2 * idx + 0] = (data >> 0) & 0xff;
796         samples[2 * idx + 1] = (data >> 8) & 0xff;
797 }
798
799 /*
800  * Local wrapper around sr_session_send() calls. Make sure to not send
801  * more samples to the session's datafeed than what was requested by a
802  * previously configured (optional) sample count.
803  */
804 static void sigma_session_send(struct sr_dev_inst *sdi,
805                                 struct sr_datafeed_packet *packet)
806 {
807         struct dev_context *devc;
808         struct sr_datafeed_logic *logic;
809         uint64_t send_now;
810
811         devc = sdi->priv;
812         if (devc->limit_samples) {
813                 logic = (void *)packet->payload;
814                 send_now = logic->length / logic->unitsize;
815                 if (devc->sent_samples + send_now > devc->limit_samples) {
816                         send_now = devc->limit_samples - devc->sent_samples;
817                         logic->length = send_now * logic->unitsize;
818                 }
819                 if (!send_now)
820                         return;
821                 devc->sent_samples += send_now;
822         }
823
824         sr_session_send(sdi, packet);
825 }
826
827 /*
828  * This size translates to: event count (1K events per cluster), times
829  * the sample width (unitsize, 16bits per event), times the maximum
830  * number of samples per event.
831  */
832 #define SAMPLES_BUFFER_SIZE     (1024 * 2 * 4)
833
834 static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
835                                       unsigned int events_in_cluster,
836                                       unsigned int triggered,
837                                       struct sr_dev_inst *sdi)
838 {
839         struct dev_context *devc = sdi->priv;
840         struct sigma_state *ss = &devc->state;
841         struct sr_datafeed_packet packet;
842         struct sr_datafeed_logic logic;
843         uint16_t tsdiff, ts, sample, item16;
844         uint8_t samples[SAMPLES_BUFFER_SIZE];
845         uint8_t *send_ptr;
846         size_t send_count, trig_count;
847         unsigned int i;
848         int j;
849
850         ts = sigma_dram_cluster_ts(dram_cluster);
851         tsdiff = ts - ss->lastts;
852         ss->lastts = ts + EVENTS_PER_CLUSTER;
853
854         packet.type = SR_DF_LOGIC;
855         packet.payload = &logic;
856         logic.unitsize = 2;
857         logic.data = samples;
858
859         /*
860          * If this cluster is not adjacent to the previously received
861          * cluster, then send the appropriate number of samples with the
862          * previous values to the sigrok session. This "decodes RLE".
863          */
864         for (ts = 0; ts < tsdiff; ts++) {
865                 i = ts % 1024;
866                 store_sr_sample(samples, i, ss->lastsample);
867
868                 /*
869                  * If we have 1024 samples ready or we're at the
870                  * end of submitting the padding samples, submit
871                  * the packet to Sigrok. Since constant data is
872                  * sent, duplication of data for rates above 50MHz
873                  * is simple.
874                  */
875                 if ((i == 1023) || (ts == tsdiff - 1)) {
876                         logic.length = (i + 1) * logic.unitsize;
877                         for (j = 0; j < devc->samples_per_event; j++)
878                                 sigma_session_send(sdi, &packet);
879                 }
880         }
881
882         /*
883          * Parse the samples in current cluster and prepare them
884          * to be submitted to Sigrok. Cope with memory layouts that
885          * vary with the samplerate.
886          */
887         send_ptr = &samples[0];
888         send_count = 0;
889         sample = 0;
890         for (i = 0; i < events_in_cluster; i++) {
891                 item16 = sigma_dram_cluster_data(dram_cluster, i);
892                 if (devc->cur_samplerate == SR_MHZ(200)) {
893                         sample = sigma_deinterlace_200mhz_data(item16, 0);
894                         store_sr_sample(samples, send_count++, sample);
895                         sample = sigma_deinterlace_200mhz_data(item16, 1);
896                         store_sr_sample(samples, send_count++, sample);
897                         sample = sigma_deinterlace_200mhz_data(item16, 2);
898                         store_sr_sample(samples, send_count++, sample);
899                         sample = sigma_deinterlace_200mhz_data(item16, 3);
900                         store_sr_sample(samples, send_count++, sample);
901                 } else if (devc->cur_samplerate == SR_MHZ(100)) {
902                         sample = sigma_deinterlace_100mhz_data(item16, 0);
903                         store_sr_sample(samples, send_count++, sample);
904                         sample = sigma_deinterlace_100mhz_data(item16, 1);
905                         store_sr_sample(samples, send_count++, sample);
906                 } else {
907                         sample = item16;
908                         store_sr_sample(samples, send_count++, sample);
909                 }
910         }
911
912         /*
913          * If a trigger position applies, then provide the datafeed with
914          * the first part of data up to that position, then send the
915          * trigger marker.
916          */
917         int trigger_offset = 0;
918         if (triggered) {
919                 /*
920                  * Trigger is not always accurate to sample because of
921                  * pipeline delay. However, it always triggers before
922                  * the actual event. We therefore look at the next
923                  * samples to pinpoint the exact position of the trigger.
924                  */
925                 trigger_offset = get_trigger_offset(samples,
926                                         ss->lastsample, &devc->trigger);
927
928                 if (trigger_offset > 0) {
929                         trig_count = trigger_offset * devc->samples_per_event;
930                         packet.type = SR_DF_LOGIC;
931                         logic.length = trig_count * logic.unitsize;
932                         sigma_session_send(sdi, &packet);
933                         send_ptr += trig_count * logic.unitsize;
934                         send_count -= trig_count;
935                 }
936
937                 /* Only send trigger if explicitly enabled. */
938                 if (devc->use_triggers) {
939                         packet.type = SR_DF_TRIGGER;
940                         sr_session_send(sdi, &packet);
941                 }
942         }
943
944         /*
945          * Send the data after the trigger, or all of the received data
946          * if no trigger position applies.
947          */
948         if (send_count) {
949                 packet.type = SR_DF_LOGIC;
950                 logic.length = send_count * logic.unitsize;
951                 logic.data = send_ptr;
952                 sigma_session_send(sdi, &packet);
953         }
954
955         ss->lastsample = sample;
956 }
957
958 /*
959  * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
960  * Each event is 20ns apart, and can contain multiple samples.
961  *
962  * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
963  * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
964  * For 50 MHz and below, events contain one sample for each channel,
965  * spread 20 ns apart.
966  */
967 static int decode_chunk_ts(struct sigma_dram_line *dram_line,
968                            uint16_t events_in_line,
969                            uint32_t trigger_event,
970                            struct sr_dev_inst *sdi)
971 {
972         struct sigma_dram_cluster *dram_cluster;
973         struct dev_context *devc;
974         unsigned int clusters_in_line;
975         unsigned int events_in_cluster;
976         unsigned int i;
977         uint32_t trigger_cluster, triggered;
978
979         devc = sdi->priv;
980         clusters_in_line = events_in_line;
981         clusters_in_line += EVENTS_PER_CLUSTER - 1;
982         clusters_in_line /= EVENTS_PER_CLUSTER;
983         trigger_cluster = ~0;
984         triggered = 0;
985
986         /* Check if trigger is in this chunk. */
987         if (trigger_event < (64 * 7)) {
988                 if (devc->cur_samplerate <= SR_MHZ(50)) {
989                         trigger_event -= MIN(EVENTS_PER_CLUSTER - 1,
990                                              trigger_event);
991                 }
992
993                 /* Find in which cluster the trigger occurred. */
994                 trigger_cluster = trigger_event / EVENTS_PER_CLUSTER;
995         }
996
997         /* For each full DRAM cluster. */
998         for (i = 0; i < clusters_in_line; i++) {
999                 dram_cluster = &dram_line->cluster[i];
1000
1001                 /* The last cluster might not be full. */
1002                 if ((i == clusters_in_line - 1) &&
1003                     (events_in_line % EVENTS_PER_CLUSTER)) {
1004                         events_in_cluster = events_in_line % EVENTS_PER_CLUSTER;
1005                 } else {
1006                         events_in_cluster = EVENTS_PER_CLUSTER;
1007                 }
1008
1009                 triggered = (i == trigger_cluster);
1010                 sigma_decode_dram_cluster(dram_cluster, events_in_cluster,
1011                                           triggered, sdi);
1012         }
1013
1014         return SR_OK;
1015 }
1016
1017 static int download_capture(struct sr_dev_inst *sdi)
1018 {
1019         const uint32_t chunks_per_read = 32;
1020
1021         struct dev_context *devc;
1022         struct sigma_dram_line *dram_line;
1023         int bufsz;
1024         uint32_t stoppos, triggerpos;
1025         uint8_t modestatus;
1026         uint32_t i;
1027         uint32_t dl_lines_total, dl_lines_curr, dl_lines_done;
1028         uint32_t dl_events_in_line;
1029         uint32_t trg_line, trg_event;
1030
1031         devc = sdi->priv;
1032         dl_events_in_line = 64 * 7;
1033         trg_line = ~0;
1034         trg_event = ~0;
1035
1036         dram_line = g_try_malloc0(chunks_per_read * sizeof(*dram_line));
1037         if (!dram_line)
1038                 return FALSE;
1039
1040         sr_info("Downloading sample data.");
1041
1042         /*
1043          * Ask the hardware to stop data acquisition. Reception of the
1044          * FORCESTOP request makes the hardware "disable RLE" (store
1045          * clusters to DRAM regardless of whether pin state changes) and
1046          * raise the POSTTRIGGERED flag.
1047          */
1048         sigma_set_register(WRITE_MODE, WMR_FORCESTOP | WMR_SDRAMWRITEEN, devc);
1049         do {
1050                 modestatus = sigma_get_register(READ_MODE, devc);
1051         } while (!(modestatus & RMR_POSTTRIGGERED));
1052
1053         /* Set SDRAM Read Enable. */
1054         sigma_set_register(WRITE_MODE, WMR_SDRAMREADEN, devc);
1055
1056         /* Get the current position. */
1057         sigma_read_pos(&stoppos, &triggerpos, devc);
1058
1059         /* Check if trigger has fired. */
1060         modestatus = sigma_get_register(READ_MODE, devc);
1061         if (modestatus & RMR_TRIGGERED) {
1062                 trg_line = triggerpos >> 9;
1063                 trg_event = triggerpos & 0x1ff;
1064         }
1065
1066         devc->sent_samples = 0;
1067
1068         /*
1069          * Determine how many 1024b "DRAM lines" do we need to read from the
1070          * Sigma so we have a complete set of samples. Note that the last
1071          * line can be only partial, containing less than 64 clusters.
1072          */
1073         dl_lines_total = (stoppos >> 9) + 1;
1074
1075         dl_lines_done = 0;
1076
1077         while (dl_lines_total > dl_lines_done) {
1078                 /* We can download only up-to 32 DRAM lines in one go! */
1079                 dl_lines_curr = MIN(chunks_per_read, dl_lines_total - dl_lines_done);
1080
1081                 bufsz = sigma_read_dram(dl_lines_done, dl_lines_curr,
1082                                         (uint8_t *)dram_line, devc);
1083                 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1084                 (void)bufsz;
1085
1086                 /* This is the first DRAM line, so find the initial timestamp. */
1087                 if (dl_lines_done == 0) {
1088                         devc->state.lastts =
1089                                 sigma_dram_cluster_ts(&dram_line[0].cluster[0]);
1090                         devc->state.lastsample = 0;
1091                 }
1092
1093                 for (i = 0; i < dl_lines_curr; i++) {
1094                         uint32_t trigger_event = ~0;
1095                         /* The last "DRAM line" can be only partially full. */
1096                         if (dl_lines_done + i == dl_lines_total - 1)
1097                                 dl_events_in_line = stoppos & 0x1ff;
1098
1099                         /* Test if the trigger happened on this line. */
1100                         if (dl_lines_done + i == trg_line)
1101                                 trigger_event = trg_event;
1102
1103                         decode_chunk_ts(dram_line + i, dl_events_in_line,
1104                                         trigger_event, sdi);
1105                 }
1106
1107                 dl_lines_done += dl_lines_curr;
1108         }
1109
1110         std_session_send_df_end(sdi);
1111
1112         sdi->driver->dev_acquisition_stop(sdi);
1113
1114         g_free(dram_line);
1115
1116         return TRUE;
1117 }
1118
1119 /*
1120  * Handle the Sigma when in CAPTURE mode. This function checks:
1121  * - Sampling time ended
1122  * - DRAM capacity overflow
1123  * This function triggers download of the samples from Sigma
1124  * in case either of the above conditions is true.
1125  */
1126 static int sigma_capture_mode(struct sr_dev_inst *sdi)
1127 {
1128         struct dev_context *devc;
1129         uint64_t running_msec;
1130         struct timeval tv;
1131         uint32_t stoppos, triggerpos;
1132
1133         devc = sdi->priv;
1134
1135         /* Check if the selected sampling duration passed. */
1136         gettimeofday(&tv, 0);
1137         running_msec = (tv.tv_sec - devc->start_tv.tv_sec) * 1000 +
1138                        (tv.tv_usec - devc->start_tv.tv_usec) / 1000;
1139         if (running_msec >= devc->limit_msec)
1140                 return download_capture(sdi);
1141
1142         /* Get the position in DRAM to which the FPGA is writing now. */
1143         sigma_read_pos(&stoppos, &triggerpos, devc);
1144         /* Test if DRAM is full and if so, download the data. */
1145         if ((stoppos >> 9) == 32767)
1146                 return download_capture(sdi);
1147
1148         return TRUE;
1149 }
1150
1151 SR_PRIV int sigma_receive_data(int fd, int revents, void *cb_data)
1152 {
1153         struct sr_dev_inst *sdi;
1154         struct dev_context *devc;
1155
1156         (void)fd;
1157         (void)revents;
1158
1159         sdi = cb_data;
1160         devc = sdi->priv;
1161
1162         if (devc->state.state == SIGMA_IDLE)
1163                 return TRUE;
1164
1165         if (devc->state.state == SIGMA_CAPTURE)
1166                 return sigma_capture_mode(sdi);
1167
1168         return TRUE;
1169 }
1170
1171 /* Build a LUT entry used by the trigger functions. */
1172 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1173 {
1174         int i, j, k, bit;
1175
1176         /* For each quad channel. */
1177         for (i = 0; i < 4; i++) {
1178                 entry[i] = 0xffff;
1179
1180                 /* For each bit in LUT. */
1181                 for (j = 0; j < 16; j++)
1182
1183                         /* For each channel in quad. */
1184                         for (k = 0; k < 4; k++) {
1185                                 bit = 1 << (i * 4 + k);
1186
1187                                 /* Set bit in entry */
1188                                 if ((mask & bit) && ((!(value & bit)) !=
1189                                                         (!(j & (1 << k)))))
1190                                         entry[i] &= ~(1 << j);
1191                         }
1192         }
1193 }
1194
1195 /* Add a logical function to LUT mask. */
1196 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1197                                  int index, int neg, uint16_t *mask)
1198 {
1199         int i, j;
1200         int x[2][2], tmp, a, b, aset, bset, rset;
1201
1202         memset(x, 0, 4 * sizeof(int));
1203
1204         /* Trigger detect condition. */
1205         switch (oper) {
1206         case OP_LEVEL:
1207                 x[0][1] = 1;
1208                 x[1][1] = 1;
1209                 break;
1210         case OP_NOT:
1211                 x[0][0] = 1;
1212                 x[1][0] = 1;
1213                 break;
1214         case OP_RISE:
1215                 x[0][1] = 1;
1216                 break;
1217         case OP_FALL:
1218                 x[1][0] = 1;
1219                 break;
1220         case OP_RISEFALL:
1221                 x[0][1] = 1;
1222                 x[1][0] = 1;
1223                 break;
1224         case OP_NOTRISE:
1225                 x[1][1] = 1;
1226                 x[0][0] = 1;
1227                 x[1][0] = 1;
1228                 break;
1229         case OP_NOTFALL:
1230                 x[1][1] = 1;
1231                 x[0][0] = 1;
1232                 x[0][1] = 1;
1233                 break;
1234         case OP_NOTRISEFALL:
1235                 x[1][1] = 1;
1236                 x[0][0] = 1;
1237                 break;
1238         }
1239
1240         /* Transpose if neg is set. */
1241         if (neg) {
1242                 for (i = 0; i < 2; i++) {
1243                         for (j = 0; j < 2; j++) {
1244                                 tmp = x[i][j];
1245                                 x[i][j] = x[1 - i][1 - j];
1246                                 x[1 - i][1 - j] = tmp;
1247                         }
1248                 }
1249         }
1250
1251         /* Update mask with function. */
1252         for (i = 0; i < 16; i++) {
1253                 a = (i >> (2 * index + 0)) & 1;
1254                 b = (i >> (2 * index + 1)) & 1;
1255
1256                 aset = (*mask >> i) & 1;
1257                 bset = x[b][a];
1258
1259                 rset = 0;
1260                 if (func == FUNC_AND || func == FUNC_NAND)
1261                         rset = aset & bset;
1262                 else if (func == FUNC_OR || func == FUNC_NOR)
1263                         rset = aset | bset;
1264                 else if (func == FUNC_XOR || func == FUNC_NXOR)
1265                         rset = aset ^ bset;
1266
1267                 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1268                         rset = !rset;
1269
1270                 *mask &= ~(1 << i);
1271
1272                 if (rset)
1273                         *mask |= 1 << i;
1274         }
1275 }
1276
1277 /*
1278  * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1279  * simple pin change and state triggers. Only two transitions (rise/fall) can be
1280  * set at any time, but a full mask and value can be set (0/1).
1281  */
1282 SR_PRIV int sigma_build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
1283 {
1284         int i,j;
1285         uint16_t masks[2] = { 0, 0 };
1286
1287         memset(lut, 0, sizeof(struct triggerlut));
1288
1289         /* Constant for simple triggers. */
1290         lut->m4 = 0xa000;
1291
1292         /* Value/mask trigger support. */
1293         build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
1294                         lut->m2d);
1295
1296         /* Rise/fall trigger support. */
1297         for (i = 0, j = 0; i < 16; i++) {
1298                 if (devc->trigger.risingmask & (1 << i) ||
1299                     devc->trigger.fallingmask & (1 << i))
1300                         masks[j++] = 1 << i;
1301         }
1302
1303         build_lut_entry(masks[0], masks[0], lut->m0d);
1304         build_lut_entry(masks[1], masks[1], lut->m1d);
1305
1306         /* Add glue logic */
1307         if (masks[0] || masks[1]) {
1308                 /* Transition trigger. */
1309                 if (masks[0] & devc->trigger.risingmask)
1310                         add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1311                 if (masks[0] & devc->trigger.fallingmask)
1312                         add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1313                 if (masks[1] & devc->trigger.risingmask)
1314                         add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1315                 if (masks[1] & devc->trigger.fallingmask)
1316                         add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1317         } else {
1318                 /* Only value/mask trigger. */
1319                 lut->m3 = 0xffff;
1320         }
1321
1322         /* Triggertype: event. */
1323         lut->params.selres = 3;
1324
1325         return SR_OK;
1326 }