295968e0f8376e52ff2faf7f7b2cc11a45e735a3
[libreriscv.git] / openpower / sv / 16_bit_compressed.mdwn
1 # 16 bit Compressed
2
3 Similar to VLE (but without immediate-prefixing) this encoding is designed
4 to fit on top of OpenPOWER ISA v3.0B when a "Modeswitch" bit is set (PCR
5 is recommended). Note that Compressed is *mutually exclusively incompatible*
6 with OpenPOWER v3.1B "prefixing" due to using (requiring) both EXT000
7 and EXT001. Hypothetically it could be made to use anything other than
8 EXT001, with some inconvenience (extra gates). The incompatibility is
9 "fixed" by swapping out of "Compressed" Mode and back into "Normal"
10 (v3.1B) Mode, at runtime, as needed.
11
12 Although initially intended to be augmented by Simple-V Prefixing (to
13 add Vector context, width overrides, e.g IEEE754 FP16, and predication) yet not put pressure on I-Cache power
14 or size, this Compressed Encoding is not critically dependent
15 *on* SV Prefixing, and may be used stand-alone.
16
17 See:
18
19 * <https://bugs.libre-soc.org/show_bug.cgi?id=238>
20 * <https://ftp.libre-soc.org/VLE_314-68105.pdf> VLE Encoding
21 * <http://lists.mailinglist.openpowerfoundation.org/pipermail/openpower-hdl-cores/2020-November/000210.html>
22
23 This one is a conundrum. OpenPOWER ISA was never designed with 16
24 bit in mind. VLE was added 10 years ago but only by way of marking
25 an entire 64k page as "VLE". With VLE not maintained it is not
26 fully compatible with current PowerISA.
27
28 Here, in order to embed 16 bit into a predominantly 32 bit stream the
29 overhead of using an entire 16 bits just to switch into Compressed mode
30 is itself a significant overhead. The situation is made worse by 6 bits
31 being taken up by Major Opcode space, leaving only 10 bits to allocate
32 to actual instructions.
33
34 Contrast this with RVC which takes 3 out of 4
35 combinations of the first 2 bits for indicating 16-bit (anything with 0b00 to 0b10 in the LSBs), and uses the 4th as a Huffman-style escape-sequence, easily allowing standard 32 bit and 16 bit to intermingle cleanly. To achieve the same thing on OpenPOWER would require a whopping 24 6-bit Major Opcodes which is clearly impractical: other schemes need to be devised.
36
37 In addition we would like to add SV-C32 which is a Vectorised version
38 of 16 bit Compressed, and ideally have a variant that adds the 27-bit
39 prefix format from SV-P64, as well.
40
41 Potential ways to reduce pressure on the 16 bit space are:
42
43 * To use more than one v3.0B Major Opcode, preferably an odd-even
44 contiguous pair
45 * To provide "paging". This involves bank-switching to alternative optimised encodings for specific workloads
46 * To enter "16 bit mode" for durations specified at the start
47 * To reserve one bit of every 16 bit instruction to indicate that the 16 bit mode is to continue to be sustained
48
49 This latter would be useful in the Vector context to have an alternative
50 meaning: as the bit which determines whether the instruction is 11-bit
51 prefixed or 27-bit prefixed:
52
53 0 1 2 3 4 5 6 7 8 9 a b c d e f |
54 |major op | 11 bit vector prefix|
55 |16 bit opcode alt vec. mode ^ |
56 | extra vector prefix if alt set|
57
58 Using a major opcode to enter 16 bit mode, leaves 11 bits to find
59 something to use them for:
60
61 0 1 2 3 4 5 6 7 8 9 a b c d e f |
62 |major op | what to do here 1 |
63 |16 bit stay in 16bit mode 1 |
64 |16 bit stay in 16bit mode 1 |
65 |16 bit exit 16bit mode 0 |
66
67 One possibility is that the 11 bits are used for bank selection, with
68 some room for additional context such as altering the registers used
69 for the 16 bit operations (bank selection of which scalar regs)
70
71 Another is to use the 11 bits for only the utmost commonly used
72 instructions. That being the case then even one of those 11 bits would
73 also need to be dedicated to saying if 16 bit mode is to be continued.
74 10 bits remain for actual opcodes, which is ridiculously tight.
75
76 The reason for picking 2 contiguous Major v3.0B opcodes is illustrated below:
77
78 |0 1 2 3 4 5 6 7 8 9 a b c d e f|
79 |major op..0| LO Half C space |
80 |major op..1| HI Half C space |
81 |N N N N N|<--11 bits C space-->|
82
83 If NNNNN is the same value (two contiguous Major v3.0B Opcodes) this saves gates at a critical part of the decode phase.
84
85 # Opcode Allocation Ideas
86
87 * one bit from the 16-bit mode is used to indicate that standard
88 (v3.0B) mode is to be dropped into for only one single instruction
89 <https://bugs.libre-soc.org/show_bug.cgi?id=238#c2>
90
91 ## Opcodes exploration (Attempt 1)
92
93 Switching between different encoding modes is controlled by M (alone)
94 in 10-bit mode, and M and N in 16-bit mode.
95
96 * M in 10-bit mode if zero indicates that following instructions are
97 standard OpenPOWER ISA 32-bit encoded (including, redundantly,
98 further 10/16-bit instructions)
99 * M in 10-bit mode if 1 indicates that following instructions are
100 in 16-bit encoding mode
101
102 Once in 16-bit mode:
103
104 * 0b01 (M=1, N=0): stay in 16-bit mode
105 * 0b00: leave 16-bit mode permanently (return to standard OpenPOWER ISA)
106 * 0b10: leave 16-bit mode for one cycle (return to standard OpenPOWER ISA)
107 * 0b11: free to be used for something completely different.
108
109 The current "top" idea for 0b11 is to use it for a new encoding format
110 of predominantly "immediates-based" 16-bit instructions (branch-conditional,
111 addi, mulli etc.)
112
113 * The Compressed Major Opcode is in bits 5-7.
114 * Minor opcode in bit 8.
115 * In some cases bit 9 is taken as an additional sub-opcode, followed
116 by bits 0-4 (for CR operations)
117 * M+N mode-switching is not available for C-Major.minor 0b001.1
118 * 10 bit mode may be expanded by 16 bit mode, adding capabilities
119 that do not fit in the extreme limited space.
120
121 Mode-switching FSM showing relationship between v3.0B, C 10bit and C 16bit.
122 16-bit immediate mode remains in 16-bit.
123
124 | 0 | 1234 | 567 8 | 9abcde | f | explanation
125 |EXT000/1 | Cmaj.m | fields | 0 | 10bit then v3.0B
126 |EXT000/1 | Cmaj.m | fields | 1 | 10bit then 16bit
127 | 0 | flds | Cmaj.m | fields | 0 | 16bit then v3.0B
128 | 0 | flds | Cmaj.m | fields | 1 | 16bit then 16bit
129 | 1 | flds | Cmaj.m | fields | 0 | 16b then 1x v3.0B
130 | 1 | flds | Cmaj.m | fields | 1 | 16b/imm then 16bit
131
132 Notes:
133
134 * Cmaj.m is the C major/minor opcode: 3 bits for major, 1 for minor
135 * EXT000 and EXT001 are v3.0B Major Opcodes. The first 5 bits
136 are zero, therefore the 6th bit is actually part of Cmaj.
137 * "10bit then 16bit" means "this instruction is encoded C 10bit
138 and the following one in C 16bit"
139
140 ### C Instruction Encoding types
141
142 10-bit Opcode formats (all start with v3.0B EXT000 or EXT001
143 Major Opcodes)
144
145 | 01234 | 567 8 | 9 | a b | c | d e | f | enc
146 | E01 | Cmaj.m | fld1 | fld2 | M | 10b
147 | E01 | Cmaj.m | offset | M | 10b b
148 | E01 | 001.1 | S1 | fd1 | S2 | fd2 | M | 10b sub
149 | E01 | 111.m | fld1 | fld2 | M | 10b LDST
150
151 16-bit Opcode formats (including 10/16/v3.0B Switching)
152
153 | 0 | 1234 | 567 8 | 9 | a b | c | d e | f | enc
154 | N | immf | Cmaj.m | fld1 | fld2 | M | 16b
155 | 1 | immf | Cmaj.m | fld1 | imm | 1 | 16b imm
156 | fd3 | 001.1 | S1 | fd1 | S2 | fd2 | M | 16b sub
157 | N | fd4 | 111.m | fld1 | fld2 | M | 16b LDST
158
159 Notes:
160
161 * fld1 and fld2 can contain reg numbers, immediates, or opcode
162 fields (BO, BI, LK)
163 * S1 and S2 are further sub-selectors of C 001.1
164
165 ### Immediate Opcodes
166
167 only available in 16-bit mode, only available when M=1 and N=1
168 and when Cmaj.min is not 0b001.1.
169
170 | 0 | 1 | 2 | 3 4 | | 567.8 | 9ab | cde | f |
171 | 1 | 0 | 0 0 0 | | 001.0 | | 000 | 1 | TBD
172 | 1 | 0 | sh2 | | 001.0 | RA | sh | 1 | sradi.
173 | 1 | 1 | 0 0 0 | | 001.0 | | 000 | 1 | TBD
174 | 1 | 1 | 0 | sh2 | | 001.0 | RA | sh | 1 | srawi.
175 | 1 | 1 | 1 | | | 001.0 | | | 1 | TBD
176 | 1 | i2 | RT | | 010.0 | RA|0 | imm | 1 | addi
177 | 1 | i2!=0 | | 010.1 | RA | imm | 1 | addis [1]
178 | 1 | 0 | 0 0 0 | | 010.1 | | | 1 | TBD
179 | 1 | i2 | | 011.0 | RA | imm | 1 | cmpdi
180 | 1 | i2 | | 011.1 | RA | imm | 1 | cmpwi
181 | 1 | i2 | | 100.0 | RT | imm | 1 | stwi
182 | 1 | i2 | | 100.1 | RT | imm | 1 | stdi
183 | 1 | i2 | | 101.0 | RA | imm | 1 | ldi
184 | 1 | i2 | | 101.1 | RA | imm | 1 | lwi
185 | 1 | i2 | RA | | 110.0 | RT | imm | 1 | fsti
186 | 1 | i2 | RA | | 110.1 | RT | imm | 1 | fstdi
187 | 1 | i2 | RT | | 111.0 | RA | imm | 1 | flwi
188 | 1 | i2 | RT | | 111.1 | RA | imm | 1 | fldi
189
190 Construction of immediate:
191
192 * [1] not the same as v3.0B addis: the shift amount is smaller and actually
193 still maps to within the v3.0B addi immediate range.
194 * addi is EXTS(i2||imm) to give a 4-bit range -8 to +7
195 * addis is EXTS(i2||imm||000) to give a 11-bit range -1024 to +1023 in increments of 8
196 * all others are EXTS(i2||imm) to give a 7-bit range -128 to +127
197 (further for LD/ST due to word/dword-alignment)
198
199 Further Notes:
200
201 * bc also has an immediate mode, listed separately below in Branch section
202 * for LD/ST, offset is aligned. 8-byte: i2||imm||0b000 4-byte: 0b00
203 * SV Prefix over-rides help provide alternative bitwidths for LD/ST
204 * RA|0 if RA is zero, addi. becomes "li"
205 - this only works if RT takes part of opcode
206 - mv is also possible by specifying an immediate of zero
207
208 ### Illegal and nop
209
210 Note that illeg is all zeros, including in the 16-bit mode.
211 Given that C is allocated to OpenPOWER ISA Major opcodes EXT000 and
212 EXT001 this ensures that in both 10-bit *and* 16-bit mode, a 16-bit
213 run of all zeros is considered "illegal" whilst 0b0000.0000.1000.0000
214 is "nop"
215
216 | 16-bit mode | | 10-bit mode |
217 | 0 | 1 | 234 | | 567.8 | 9 ab | c de | f |
218 | 0 | 0 000 | | 000.0 | 0 00 | 0 00 | 0 | illeg
219 | 0 | 0 000 | | 000.0 | 0 00 | 0 00 | 1 | nop
220
221 16 bit mode only:
222
223 | 1 | 0 000 | | 000.0 | 0 00 | 0 00 | 0 | nop
224 | 1 | nonzero | | 000.0 | 0 00 | 0 00 | 0 | TBD
225
226 Notes:
227
228 * All-zeros being an illegal instruction is normal for ISAs. Ensuring that
229 this remains true at all times i.e. for both 10 bit and 16 bit mode is
230 common sense.
231 * The 10-bit nop (bit 15, M=1) is intended for circumstances
232 where alignment to 32-bit before returning to v3.0B is required.
233 M=1 being an indication "return to Standard v3.0B Encoding Mode".
234 * The 16-bit nop (bit 0, N=1) is intended for circumstances where a
235 return to Standard v3.0B Encoding is required for one cycle
236 but one cycle where alignment to a 32-bit boundary is needed.
237 Examples of this would be to return to "strict" (non-C) mode
238 where the PC may not be on a non-word-aligned boundary.
239 * If for any reason multiple 16 bit nops are needed in succession
240 the M=1 variant can be used, because each one returns to
241 Standard v3.0B Encoding Mode, each time.
242
243 In essence the 2 nops are needed due to there being 2 different C forms: 10 and 16 bit.
244
245 ### Branch
246
247 | 16-bit mode | | 10-bit mode |
248 | 0 | 1 | 234 | | 567.8 | 9 ab | c de | f |
249 | N | offs2 | | 000.LK | offs!=0 | M | b, bl
250 | 1 | offs2 | | 000.LK | BI | BO1 oo | 1 | bc, bcl
251 | N | BO3 BI3 | | 001.0 | LK BI | BO | M | bclr, bclrl
252
253 16 bit mode:
254
255 * bc only available when N,M=0b11
256 * offs2 extends offset in MSBs
257 * BI3 extends BI in MSBs to allow selection of full CR
258 * BO3 extends BO
259 * bc offset constructed from oo as LSBs and offs2 as MSBs
260 * bc BI allows selection of all bits from CR0 or CR1
261 * bc CR check is always active (as if BO0=1) therefore BO1 inverts
262
263 10 bit mode:
264
265 * illegal (all zeros) covers part of branch (offs=0,M=0,LK=0)
266 * nop also covers part of branch (offs=0,M=0,LK=1)
267 * bc **not available** in 10-bit mode
268 * BO[0] enables CR check, BO[1] inverts check
269 * BI refers to CR0 only (4 bits of)
270 * no Branch Conditional with immediate
271 * no Absolute Address
272 * CTR mode allowed with BO[2] for b only.
273 * offs is to 2 byte (signed) aligned
274 * all branches to 2 byte aligned
275
276 ### LD/ST
277
278 | 16-bit mode | | 10-bit mode |
279 | 0 | 1 | 2 3 4 | | 567.8 | 9 a b | c d e | f |
280 | RA2 | SZ | RB | | 001.1 | 1 RA | 0 RT | M | st
281 | RA2 | SZ | RB | | 001.1 | 1 RA | 1 RT | M | fst
282 | N | SZ | RT | | 111.0 | RA | RB | M | ld
283 | N | SZ | RT | | 111.1 | RA | RB | M | fld
284
285 * elwidth overrides can set different widths
286
287 16 bit mode:
288
289 * SZ=1 is 64 bit, SZ=0 is 32 bit
290 * RA2 extends RA to 3 bits (MSB)
291 * RT2 extends RT to 3 bits (MSB)
292
293 10 bit mode:
294
295 * RA and RB are only 2 bit (0-3)
296 * for LD, RT is implicitly RB: "ld RT=RB, RA(RB)"
297 * for ST, there is no offset: "st RT, RA(0)"
298
299 ### Arithmetic
300
301 | 16-bit mode | | 10-bit mode |
302 | 0 | 1 | 234 | | 567.8 | 9ab | c d e | f |
303 | N | 0 | RT | | 010.0 | RB | RA!=0 | M | add
304 | N | 0 | RT | | 010.1 | RB | RA|0 | M | sub.
305 | N | 0 | BF | | 011.0 | RB | RA|0 | M | cmpl
306
307 Notes:
308
309 * sub. and cmpl: default CR target is CR0
310 * for (RA|0) when RA=0 the input is a zero immediate,
311 meaning that sub. becomes neg. and cmp becomes cmpi against zero
312 * RT is implicitly RB: "add RT(=RB), RA, RB"
313 * Opcode 0b010.0 RA=0 is not missing from the above:
314 it is a system-wide instruction, "cbank" (section below)
315
316 16 bit mode only:
317
318 | 0 | 1 | 234 | | 567.8 | 9ab | cde | f |
319 | N | 1 | RA | | 010.0 | RB | RS | 0 | sld.
320 | N | 1 | RA | | 010.1 | RB | RS!=0 | 0 | srd.
321 | N | 1 | RA | | 010.1 | RB | 000 | 0 | srad.
322 | N | 1 | BF | | 011.0 | RB | RA|0 | 0 | cmpw
323
324 Notes:
325
326 * for srad, RS=RA: "srad. RA(=RS), RS, RB"
327
328
329 ### Logical
330
331 | 16-bit mode | | 10-bit mode |
332 | 0 | 1 | 2 3 4 | | 567.8 | 9ab | c d e | f |
333 | N | 0 | RT | | 100.0 | RB | RA!=0 | M | and
334 | N | 0 | RT | | 100.1 | RB | RA!=0 | M | nand
335 | N | 0 | RT | | 101.0 | RB | RA!=0 | M | or
336 | N | 0 | RT | | 101.1 | RB | RA!=0 | M | nor
337 | N | 0 | RT | | 100.0 | RB | 0 0 0 | M | extsw
338 | N | 0 | RT | | 100.1 | RB | 0 0 0 | M | cntlz
339 | N | 0 | RT | | 101.0 | RB | 0 0 0 | M | popcnt
340 | N | 0 | RT | | 101.1 | RB | 0 0 0 | M | not
341
342 16-bit mode only:
343
344 | 0 | 1 | 2 3 4 | | 567.8 | 9ab | c d e | f |
345 | N | 1 | RT | | 100.0 | RB | RA!=0 | 0 | TBD
346 | N | 1 | RT | | 100.1 | RB | RA!=0 | 0 | TBD
347 | N | 1 | RT | | 101.0 | RB | RA!=0 | 0 | xor
348 | N | 1 | RT | | 101.1 | RB | RA!=0 | 0 | eqv (xnor)
349 | N | 1 | RT | | 100.0 | RB | 0 0 0 | 0 | extsb
350 | N | 1 | RT | | 100.1 | RB | 0 0 0 | 0 | cnttz
351 | N | 1 | RT | | 101.0 | RB | 0 0 0 | 0 | TBD
352 | N | 1 | RT | | 101.1 | RB | 0 0 0 | 0 | extsh
353
354 10 bit mode:
355
356 * for (RA|0) when RA=0 the input is a zero immediate,
357 meaning that nor becomes not
358 * cntlz, popcnt, exts **not available** in 10-bit mode
359 * RT is implicitly RB: "and RT(=RB), RA, RB"
360
361 ### Floating Point
362
363 Note here that elwidth overrides (SV Prefix) can be used to select FP16/32/64
364
365 | 16-bit mode | | 10-bit mode |
366 | 0 | 1 | 2 3 4 | | 567.8 | 9ab | c d e | f |
367 | N | | RT | | 011.1 | RB | RA!=0 | M | fsub.
368 | N | 0 | RT | | 110.0 | RB | RA!=0 | M | fadd
369 | N | 0 | RT | | 110.1 | RB | RA!=0 | M | fmul
370 | N | 0 | RT | | 011.1 | RB | 0 0 0 | M | fneg.
371 | N | 0 | RT | | 110.0 | RB | 0 0 0 | M |
372 | N | 0 | RT | | 110.1 | RB | 0 0 0 | M |
373
374 16-bit mode only:
375
376 | 0 | 1 | 2 3 4 | | 567.8 | 9ab | c d e | f |
377 | N | 1 | RT | | 011.1 | RB | RA!=0 | 0 |
378 | N | 1 | RT | | 110.0 | RB | RA!=0 | 0 |
379 | N | 1 | RT | | 110.1 | RB | RA!=0 | 0 | fdiv
380 | N | 1 | RT | | 011.1 | RB | 0 0 0 | 0 | fabs.
381 | N | 1 | RT | | 110.0 | RB | 0 0 0 | 0 | fmr.
382 | N | 1 | RT | | 110.1 | RB | 0 0 0 | 0 |
383
384 16 bit only, FP to INT convert
385
386 | 0123 | 4 | | 567.8 | 9 ab | cde | f |
387 | 0010 | X | | 001.1 | 0 RA | Y RT | M | fp2int
388 | 0011 | X | | 001.1 | 0 RA | Y RT | M | int2fp
389
390 * X: signed=1, unsigned=0
391 * Y: FP32=0, FP64=1
392
393 10 bit mode:
394
395 * fsub. fneg. and fmr. default target is CR1
396 * fmr. is **not available** in 10-bit mode
397 * fdiv is **not available** in 10-bit mode
398
399 16 bit mode:
400
401 * fmr. copies RB to RT (and sets CR1)
402
403 ### Condition Register
404
405 | 16-bit mode | | 10-bit mode |
406 | 0 1 2 3 | 4 | | 567.8 | 9 ab | cde | f |
407 | 0 0 0 0 | BF2 | | 001.1 | 0 BF | BFA | M | mcrf
408 | 0 0 0 1 | BA2 | | 001.1 | 0 BA | BB | M | crnor
409 | 0 1 0 0 | BA2 | | 001.1 | 0 BA | BB | M | crandc
410 | 0 1 1 0 | BA2 | | 001.1 | 0 BA | BB | M | crxor
411 | 0 1 1 1 | BA2 | | 001.1 | 0 BA | BB | M | crnand
412 | 1 0 0 0 | BA2 | | 001.1 | 0 BA | BB | M | crand
413 | 1 0 0 1 | BA2 | | 001.1 | 0 BA | BB | M | creqv
414 | 1 1 0 1 | BA2 | | 001.1 | 0 BA | BB | M | crorc
415 | 1 1 1 0 | BA2 | | 001.1 | 0 BA | BB | M | cror
416
417 10 bit mode:
418
419 * mcrf BF is only 2 bits which means the destination is only CR0-CR3
420 * CR operations: **not available** in 10-bit mode (but mcrf is)
421
422 16 bit mode:
423
424 * mcrf BF2 extends BF (in MSB) to 3 bits
425 * CR operations: destination register is same as BA.
426 * CR operations: only possible on CR0 and CR1
427
428 SV (Vector Mode):
429
430 * CR operations: greatly extended reach/range (useful for predicates)
431
432 ### System
433
434 cbank: Selection of Compressed-encoding "Bank". Different "banks"
435 give different meanings to opcodes. Example: CBank=0b001 is heavily
436 optimised to A/Video Encode/Decode. cbank borrows from add's encoding
437 space (when RA==0)
438
439 | 16-bit mode | | 10-bit mode |
440 | 0 | 1 2 3 4 | | 567.8 | 9ab | cde | f |
441 | N | 0 Bank2 | | 010.0 | CBank | 000 | M | cbank
442
443 **not available** in 10-bit mode:
444
445 | 0 1 2 3 | 4 | | 567.8 | 9 ab | cde | f |
446 | 1 1 1 1 | 0 | | 001.1 | 0 00 | RT | M | mtlr
447 | 1 1 1 1 | 0 | | 001.1 | 0 01 | RT | M | mtctr
448 | 1 1 1 1 | 0 | | 001.1 | 0 11 | RT | M | mtcr
449 | 1 1 1 1 | 1 | | 001.1 | 0 00 | RA | M | mflr
450 | 1 1 1 1 | 1 | | 001.1 | 0 01 | RA | M | mfctr
451 | 1 1 1 1 | 1 | | 001.1 | 0 11 | RA | M | mfcr
452
453 ### Unallocated
454
455 | 0 1 2 3 | 4 | | 567.8 | 9 ab | cde | f |
456 | 0 1 0 1 | | | 001.1 | 0 | | M |
457 | 1 0 1 0 | | | 001.1 | 0 | | M |
458 | 1 0 1 1 | | | 001.1 | 0 | | M |
459 | 1 1 0 0 | | | 001.1 | 0 | | M |
460 | 1 1 1 1 | | | 001.1 | 0 10 | | M |