File: bitstream_format.rst

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (171 lines) | stat: -rw-r--r-- 11,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Bitstream format
================

Some documentation on the ECP5 bitstream format is published by Lattice themselves
in the ECP5 sysCONFIG Usage Guide (FPGA-TN-02039_).

.. _FPGA-TN-02039: https://www.latticesemi.com/-/media/LatticeSemi/Documents/ApplicationNotes/EH/FPGA-TN-02039-1-7-ECP5-and-ECP5-5G-sysCONFIG.pdf

Basic Structure
----------------

The ECP5 is primarily byte oriented and always byte aligned. Multi-byte words are always in big endian format.

Before the start of the bitstream itself is a comment section, which starts with FF 00 and 00 FF.
Inside it are several null-terminated strings used as metadata by Lattice. The start of the bitstream
is demarcated by a preamable, 0xFFFFBDB3. This is then followed by a 0xFFFFFFFF dummy section and then the
start of functional commands.

At minimum, a bitstream command is an 8 bit command type field, then 24 bits of command information.
The MSB of command information is set to 1 if a CRC16 follows the command. The other 23 bits are command-specific,
but usually zeros. Then follows a command-specific number of payload bytes, and the CRC16 if applicable.

The CRC16 is accumulated over all commands until a CRC16 check is reached. It is not reset at the end of commands
without a CRC16 check - except the ``LSC_RESET_CRC`` command, and after the actual bitstream payload
(``LSC_PROG_INCR_RTI`` or ``LSC_PROG_INCR_CMP``).

The CRC16 is calculated using the polynomial 0x8005 with no bit reversal. This algorithm is sometimes known as
"CRC16-BUYPASS".

    NB: Lattice's documents talk about the CRC16 being flipped. This is based on standard
    CRC16 code with reversal, effectively performing double bit reversal. CRC16 code with no
    bit reversal at all matches the actual format.

Control Commands
------------------
Control commands seen in a typical uncompressed, unencrypted bitstream are:

+-------------------------------+-----+--------------------------+---------------------------------------------------+
| Command                       | Hex | Parameters               | Description                                       |
+==========================+====+=====+==========================+===================================================+
| Dummy                         | FF  | N/A                      | Ignored, used for padding                         |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``LSC_RESET_CRC``             | 3B  | 24 bit info: all 0       | Resets the CRC16 register                         |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``VERIFY_ID``                 | E2  | - 24 bit info: all 0     | This checks the actual device ID against the given|
|                               |     | - 32 bit device JTAG ID  | value and fails if they do not match.             |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``LSC_WRITE_COMP_DIC``        | 02  | - 24 bit info: all 0     | This stores the 8 most common bytes in the frames |
|                               |     | - 8 bit Pattern7         |                                                   |
|                               |     | - ... (6 more patterns)  |                                                   |
|                               |     | - 8 bit Pattern0         |                                                   |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``LSC_PROG_CNTRL0``           | 22  | - 24 bit info: all 0     | This sets the value of device control register 0  |
|                               |     | - 32 bit CtlReg0 value   | Normally 0x40000000                               |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``LSC_INIT_ADDRESS``          | 46  | 24 bit info: all 0       | Resets the frame address register                 |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``LSC_WRITE_ADDRESS``         | B4  | - 24 bit info: all 0     | Loads the frame address register                  |
|                               |     | - 32 bit frame address   |                                                   |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``ISC_PROGRAM_SECURITY``      | CE  | 24 bit info: all 0       | Program the security bit (prevents readback (?) ) |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``ISC_PROGRAM_USERCODE``      | C2  | - CRC bit set, 23 bits 0 | Sets the value of the USERCODE register           |
|                               |     | - 32 bit USERCODE value  |                                                   |
+-------------------------------+-----+--------------------------+---------------------------------------------------+
| ``ISC_PROGRAM_DONE``          | 5E  | 24 bit info: all 0       | End of bitstream, set DONE high                   |
+-------------------------------+-----+--------------------------+---------------------------------------------------+

Configuration Data
-------------------
The FPGA configuration data itself is programmed by using command ``LSC_PROG_INCR_RTI`` (0x82) if no compression is
used and command ``LSC_PROG_INCR_CMP`` (0xB8) when using compression. Following either of these commands,
there are some setup bits:

 - 1 bit: CRC16 comparison flag, normally set
 - 1 bit: CRC16 comparison at end flag, normally cleared = CRC check after every frame
 - 1 bit: dummy bits setting, normally cleared = include dummy bits in bitstream
 - 1 bit: dummy byte setting, normally cleared = use following bits as number of dummy bytes
 - 4 bits: number of dummy bytes between frames, usually 1
 - 16 bits: number of configuration frames

This is then followed by a number of frames, each in the following format:

 - The configuration frame itself (compressed in the case of the  ``LSC_PROG_INCR_RTI`` command),
   such that bit 0 bit 0 of the first byte is the MSB of the frame, bit 7 of the first byte the
   MSB-7 and bit 0 of the last byte (if there are no dummy bits) being the LSB of the frame.
 - Any dummy bits needed to pad the frame to a whole number of bytes.
 - If the second flag is cleared (see above) a CRC-16 checksum:
    - For the first frame, this also covers any other commands sent before the programming command
      but after a CRC reset, and the programming command itself.
    - For subsequent frames, this excludes dummy bytes between frames
 - Dummy 0xFF bytes, usually only 1

The highest numbered frame in the chip is sent first.

If the second flag is set there's no CRC sent in between frames but there's still one CRC-16 checksum
after all the frames (this also covers any other commands sent before the programming command but after a CRC reset,
and the programming command itself.).

Separate commands are used if EBR needs to be configured in the bitstream. EBR data can't use compression.
``EBR_ADDRESS`` (0xF6) is used to select the EBR to program and the starting address in the EBR;
and ``LSC_EBR_WRITE`` (0xB2) is used to program the EBR itself using 72-bit frames. The specifics of these
still need to be documented.

Compression Algorithm
------------------------------

 - Before compression, the frame is left padded with zeroes (0) to make the data frame 64-bit bounded.
 - After compressing the frame data, the resulting bits are right padded with zeroes (0)
   to make the dataframe byte bounded.

After padding, every byte in the bitstream is compressed by a simple prefix-free code with just 4 cases:

+--------------+--------------+--------+---------------------+
| Code         | Argument     | Length | Encoded byte        |
+==============+==============+========+=====================+
| 0            |              | 1      | zero                |
+--------------+--------------+--------+---------------------+
| 100xxx       | bit position | 6      | byte with 1 bit set |
+--------------+--------------+--------+---------------------+
| 101xxx       | byte index   | 6      | stored byte         |
+--------------+--------------+--------+---------------------+
| 11xxxxxxxx   | lit. byte    | 10     | all others          |
+--------------+--------------+--------+---------------------+

- The first case is for the byte zero (``00000000``).  That's just represented by a single zero bit (``0``).
- The second case is for bytes with just one bit set.  After a ``100`` the set bit position is encoded
  in the following 3 bits.  For example the byte ``00100000`` is encoded as ``100101`` because only the
  bit 5 is set (with 0 being the lsb and 7 the msb).
- The third case is for selecting one of the bytes stored by the ``LSC_WRITE_COMP_DIC`` instruction.  Those
  bytes are selected as the 8 most common bytes (ignoring the zero bytes and the bytes with just one bit set,
  because those are encoded with the two previous cases).  After a ``101`` the number of the selected pattern
  is encoded with 3 bits.  For example to select pattern3 the code would be ``101011``.
- The fourth case is for all remaining bytes.  In that case after a ``11`` the complete byte is copied.  For example
  byte ``11001010`` would be encoded as ``1111001010``.

Partial Bitstreams
------------------------------

``LSC_WRITE_ADDRESS`` can be used to make partial bitstreams. Combined with background reconfiguration and
the ability to reload frames glitchlessly; partial reconfiguration is possible on ECP5.

``LSC_WRITE_ADDRESS`` takes a frame address; however frame addressing is not strictly linear. It has only
been fully documented for the 45k device and is as follows:

 - the first 7 bits are always between 0 and 105 (each group of 106 frames is a column)
 - the next 5 bits are the column index within a tap region
 - the MSBs from bit 12 onwards are the tap region index

To enable loading of partial bitstreams the ``BACKGROUND_RECONFIG`` sysCONFIG option must be set. Then, to
avoid reinitialising the whole device, instructions 0x79 with no data and 0x74 followed by 0x00 must be
sent over JTAG before the partial bitstream data.

Device-Specific Information
------------------------------

+-----------+-------------+--------+-----------------------+----------------------+
| Device    | Device ID   | Frames | Config Bits per Frame | Dummy Bits per Frame |
+===========+=============+========+=======================+======================+
| LFE5U-25  | 0x41111043  | 7562   | 592                   | 0                    |
+-----------+-------------+--------+-----------------------+----------------------+
| LFE5UM-25 |  0x01111043 | 7562   | 592                   | 0                    |
+-----------+-------------+--------+-----------------------+----------------------+
| LFE5U-45  | 0x41112043  | 9470   | 846                   | 2                    |
+-----------+-------------+--------+-----------------------+----------------------+
| LFE5UM-45 | 0x01112043  | 9470   | 846                   | 2                    |
+-----------+-------------+--------+-----------------------+----------------------+
| LFE5U-85  | 0x41113043  | 13294  | 1136                  | 0                    |
+-----------+-------------+--------+-----------------------+----------------------+
| LFE5UM-85 | 0x01113043  | 13294  | 1136                  | 0                    |
+-----------+-------------+--------+-----------------------+----------------------+